问题描述
我的IQueryable< SomeClass的>
baseList
和列表< someOtherClass>
someData p>
我想要做的就是更新属性在baseList一些项目。
对于someData每一个项目,我想找到baselist相应的项目并更新项目的属性。
someOtherClass.someCode == baseList.myCode
我可以做一些类型的LINQ加入,并设置baseList.someData + = someOtherClass.DataIWantToConcantenate的。
我大概可以通过反复这样做,但有一个奇特的LINQ的方法,我可以在短短的几行代码做到这一点?
感谢您的任何提示,
〜CK在圣地亚哥
要配对的两个列表元素,你可以使用LINQ加入:
从D VAR对=以someData
JOIN b在baseList.AsEnumerable()上d.someCode
等于b.myCode
。选择所需的新{b,D};
这会给你每一个项目的枚举 someData code>在
baseList
。从那里,你可以在一个循环串联:
的foreach(VAR对成对)
pair.b.SomeData + = pair.d.DataIWantToConcantenate;
如果你真正的意思一套级联,而不是 + =
,看看LINQ的联盟,相交或除的方法。
I have IQueryable<someClass>
baseList
and List<someOtherClass>
someData
What I want to do is update attributes in some items in baseList.
For every item in someData, I want to find the corresponding item in baselist and update a property of the item.
someOtherClass.someCode == baseList.myCode
can I do some type of join with Linq and set baseList.someData += someOtherClass.DataIWantToConcantenate.
I could probably do this by iteration, but is there a fancy Linq way I can do this in just a couple lines of code?
Thanks for any tips,~ck in San Diego
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.
这篇关于LINQ的更新与另一个集合值的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!