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;
}
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
– Ricardo
Really the idea is to use the
ForEach
shown in the answer indicated, however, my query brings together data,Produtos
,orcItens
, when usingForEach
ex:produtos.ForEach
cannot access budget data, in my example would beo.Quantidade
.– rubStackOverflow
It’s because you’re doing
select p
wherep
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. Thereselect
you can return anew MEUOBJETO
and so have the data as you want.– Ricardo
Entedi, thanks for the tip.
– rubStackOverflow