ahsatan ahsatan -4 years ago 78
C# Question

Need help sorting using LINQ Lambda C#

I have a List of Items: List items;
Each Item has a property Item #, LongID and InventoyBranchItem.
Each InventoyBranchItem has Item #, Item Quantity and BranchCode.

Item

Item # Long ID ItemInventoryLoc
123456 ANBCDDFD 1
999999 BBBBBBBB 1
777777 ATSDFDDF 1


ItemInventoryLoc

Item# Quantity BranchCode
123456 6 T1
999999 8 T3
777777 2 T2


I need to sort by BranchCode then by LongID. I am having trouble trying to do so with lambda.

List<Item> sortedList = items.OrderBy(x => x.ItemInventory....)
.thenBy(x => x.LongID).ToList();


I don't know what syntax to use to get to the BranchCode.

Answer Source

If you're sure an Item is always related o an InventoryBrachItem, then I guess you could do this to get the BranchCode in your OrderBy:

var sortedList = items.OrderBy(x => x.InventoryBrachItems.FirstOrDefault().BranchCode)
                      .ThenBy(x=> x.LongItemNumber);

But you need to explain more the logic you're pursuing to order your items, because it can be related with more than one InventoryBranchItem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download