Linq to update a collection with values from another collection?

前端 未结 5 1189
北恋
北恋 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:29

    To pair elements in the two lists you can use a LINQ join:

    var pairs = from d in someData
                join b in baseList.AsEnumerable()
                    on d.someCode equals b.myCode
                select new { b, d };
    

    This will give you an enumeration of each item in someData paired with its counterpart in baseList. From there, you can concatenate in a loop:

    foreach(var pair in pairs)
        pair.b.SomeData += pair.d.DataIWantToConcantenate;
    

    If you really meant set concatenation rather than +=, take a look at LINQ's Union, Intersect or Except methods.

提交回复
热议问题