momokjaaaaa
momokjaaaaa

Reputation: 1303

how to convert foreach to linq to get the element

How can I convert below foreach to linq? I need to get the element.

foreach (var x in LoadData)
                item.Comments = x.Inventory.Comments;

Upvotes: 0

Views: 79

Answers (1)

Boris Parfenenkov
Boris Parfenenkov

Reputation: 3279

Maybe, you want to use Select function:

LoadData.Select(x => x.Inventory.Comments);

Edit: And if you want to get last element, you can use Last function:

LoadData.Select(x => x.Inventory.Comments).Last();

Or

LoadData.Last().Inventory.Comments;

Upvotes: 2

Related Questions