Update data using LINQ

Asked

Viewed 351 times

5

I read that LINQ should be used for queries, but in the context below, the best way I found to update was by using the let.

var produtos = from o in orcItens    
    join p in Produtos on o.Id equals p.Id   
    let x = p.Estoque -= o.Quantidade
    select p;
  • Produtos is a class IQueryable<> that will be persisted.
  • orcItens is a list of ids, quantidade of the products sold

There is another way to update the data using lambda, for example?

I tried to use foreach but I could not get the data grouped.

  • 1

    It is possible to do a lot with Ingl, an example very close to what you want can be seen here http://stackoverflow.com/a/14139472/3517631, within the loop you can develop the idea, but try to give a better idea of the doubt, then it would be easier to formulate a good answer

  • Really the idea is to use the ForEach shown in the answer indicated, however, my query brings together data, Produtos, orcItens, when using ForEach ex: produtos.ForEach cannot access budget data, in my example would be o.Quantidade.

  • It’s because you’re doing select p where p is product, so the consultation does not access budget. I don’t know the whole system, but I suggest creating another class to do the business treatment on top of these objects. There select you can return a new MEUOBJETO and so have the data as you want.

  • Entedi, thanks for the tip.

2 answers

2

LINQ -> Language-Integrated QUERY

Query -> Query

It’s even possible, but LINQ was created to make queries. Do not try to subvert your function, especially without deeply mastering its functioning and the consequences of doing this.

Go of foreach (command) and be happy. Obviously you can use LINQ to select what you will update.

Be careful not to complicate the design of the application just to facilitate the use of LINQ. Do the best for the model, if LINQ does not fit in it, bad luck of LINQ.

2


LINQ is a query-only language, it "does not allow" you to make changes to data collections.

So to solve your problem the ideal would be to make a query for the "products" that correspond to the "budgets", put this result in an object and at the end of the query join all these objects in a collection. Then I would use a foreach in this collection to update the data.

See the example below for how to do this. Suppose you have the classes below:

public class Produto
{
    public int Id { get; set; }
    public int Estoque { get; set; }
}

public class Orcamento
{
    public int Id { get; set; }
    public int Quantidade { get; set; }
}

Set a class to make the association between the "product" and the corresponding "budget":

public class ProdutoOrcamento
{
    public Produto Produto { get; set; }
    public Orcamento Orcamento { get; set; }
}

Make the query, store the result, and through a foreach update the data in the Product instance:

var produtosOrcamentos = (from o in orcamentos
                          join p in produtos on o.Id equals p.Id
                          select new ProdutoOrcamento { Produto = p, Orcamento = o });

foreach (var i in produtosOrcamentos)
{
    i.Produto.Estoque = i.Orcamento.Quantidade;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.