嗨,我是新来的c#和Linq
.如果某个属性的值与其他lists属性的值匹配,我试图更新一个列表.
假设我有一个列表 firstlist
Name code
ABC 101
DEF 201
GHI 301
JKL 401
MNO 101
Run Code Online (Sandbox Code Playgroud)
第二个列表为 secondlist
description Code
IT 101
ADMIN 201
Develeopment 301
Testing 401
Service 501
Run Code Online (Sandbox Code Playgroud)
我希望我的结果列表为
Name code
ABC 101 IT
DEF 201 ADMIN
GHI 301 Develeopment
JKL 401 Testing
MNO 101 IT
Run Code Online (Sandbox Code Playgroud)
我试过这样的事,
var query = firstlist.Select(x => { x.Code = 101; return x.Code.ToString()+"IT"; })
Run Code Online (Sandbox Code Playgroud)
但我想匹配来自secondlist
代替的101
代码和匹配代码我想更新firstlist
代码code+description
我不知道怎么做如果有人建议任何方式或链接将指导我将是伟大的.
*******更新*******
我想说的是@Sergey建议的那样
from x in firstlist
join y in secondList
on x.code equals y.Code
select new {
x.Name,
code = String.Format("{0} {1}", y.Code, y.description)
}
Run Code Online (Sandbox Code Playgroud)
代替这个我可以做这样的事情
from x in firstlist
join y in secondList
on x.code equals y.Code
select x.code = String.Format("{0} {1}", y.Code, y.description)
Run Code Online (Sandbox Code Playgroud)
这将只更新现有listone
的代替每场比赛的创建新实体
使用Enumerable.Join获取两个列表中匹配的项目:
from x in firstlist
join y in secondList
on x.code equals y.Code
select new {
x.Name,
code = String.Format("{0} {1}", y.Code, y.description)
}
Run Code Online (Sandbox Code Playgroud)
更新第一个列表中的对象:
var query = from x in firstlist
join y in secondList
on x.code equals y.Code
select new { x, y };
foreach(var match in query)
match.x.Code = String.Format("{0} {1}", match.y.Code, match.y.description);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17049 次 |
最近记录: |