Linq to update a collection with values from another collection?

前端 未结 5 1185
北恋
北恋 2020-12-31 04:05

I have IQueryable baseList

and List someData

What I want to do is update attributes in some

5条回答
  •  独厮守ぢ
    2020-12-31 04:23

    You can convert the IQueryable into a List, use the ForEach method to loop over it and update the elements, then convert back to IQueryable:

    List convertedList = baseList.ToList();
    convertedList.ForEach(sc =>
    {
        SomeOtherClass oc = someData.First(obj => obj.SomeCode == sc.MyCode);
        if (oc != null)
        {
            sc.SomeData += oc.DataIWantToConcatenate;
        }
    });
    
    baseList = convertedList.AsQueryable(); // back to IQueryable
    

    But it may be more efficient during this using non-LINQ constructs.

提交回复
热议问题