C# List grouping and assigning a value

前端 未结 2 423
花落未央
花落未央 2021-01-24 06:26

I have a list of Orders. This list contains multiple orders for the same item, see the table below.

I then want to assign each item that is the same (i.e. ABC) the same

2条回答
  •  伪装坚强ぢ
    2021-01-24 06:58

    You can do this that way, it will assign same blockid for same orderid

    var ordered = listOrder.GroupBy(x => x.OrderId).ToList();
    for (int i = 0; i < ordered.Count(); i++)
    {
        ordered[i].ForEach(x=>x.BlockId=i+1);
    }   
    

    it will group orders by orderid then assign each group next blockid. Note that it won't be done fully in linq, because linq is for querying not changing data.

提交回复
热议问题