LINQ相交或加入返回从一个集合,具有匹配性到另一个项目? [英] Linq intersect or join to return items from one collection that have matching properties to another?
问题描述
我有以下情形:其中会包含3匹配属性不同的类型
的两个列表(在现实中,名称是不一样的,因为它们是从不同的系统/数据库表,但它们的内容匹配)。
在我的例子,我有一个名为性质相同的只是为了使它更容易!
我想获得前缀+数字+后缀列表的帐户
那里是在查找匹配项(注意:查找可以包含相同的价值观多次 - 在对象属性的其余部分是不同的)
这是我目前使用的代码,但感觉笨重。是否有一个更清洁,更好的方式来acheive同样的结果?我试着包含(),但不知道如何限制所有的三个属性。
VAR账户=新的List<帐户> {
新帐户{PREFIX =001,编号=10101,后缀=666},
新帐户{PREFIX =001,编号=10202,后缀= 777},
新帐户{PREFIX =001,编号=10303,后缀=777},
新帐户{PREFIX =002,编号=20101后缀=666},
新帐户{PREFIX =002,编号=20101,后缀=777}
};
变种查找=新的List<查找> {
新的查找{PREFIX =001,编号=10101,后缀=666},
新的查找{ PREFIX =001,编号=10101,后缀=666},
新的查找{PREFIX =002,编号=20101,后缀=666},
新查找{PREFIX =001,编号=10101,后缀=666},
};
VAR匹配=((从账户中的
选择查找
从一个)
.Intersect(由L占
,其中l。前缀== a.Prefix&放大器;&安培;
l.Number == a.Number&放大器;&安培;
l.Suffix == a.Suffix
选择)
) 。选择(A =>的String.Format({0} {1} {2},a.Prefix,a.Number,a.Suffix));
您可以使用下面的代码来获取匹配:
VAR的匹配=(从账户中的
选择新{P = a.Prefix,N = a.Number ,S = a.Suffix})
.Intersect(由L中查找
选择新{P = l.Prefix,N = l.Number,S = l.Suffix})
。选择(T =>的String.Format({0} {1} {2},总磷,总氮,TS));;
I have the following scenario: Two lists of different Types
which happen to contain 3 matching properties (in reality, the names are not the same as they are from different systems/database tables, but their contents match).
In my example I have named the properties the same just to make it easier!
I'd like to get a list of Prefix+Number+Suffix
for accounts where there is a matching item in lookup (NOTE: Lookup can contain the same values multiple times - the rest of the properties in the objects are different)
This is the code I am currently using, but it feels clunky. Is there a cleaner, better way to acheive the same result? I tried "Contains()" but wasn't sure how to restrict to all three properties.
var accounts = new List<Account>{
new Account{Prefix="001", Number="10101", Suffix="666"},
new Account{Prefix="001", Number="10202", Suffix="777"},
new Account{Prefix="001", Number="10303", Suffix="777"},
new Account{Prefix="002", Number="20101", Suffix="666"},
new Account{Prefix="002", Number="20101", Suffix="777"}
};
var lookup = new List<Lookup>{
new Lookup{Prefix="001", Number="10101", Suffix="666"},
new Lookup{Prefix="001", Number="10101", Suffix="666"},
new Lookup{Prefix="002", Number="20101", Suffix="666"},
new Lookup{Prefix="001", Number="10101", Suffix="666"},
};
var match = ((from a in accounts
select a)
.Intersect(from l in lookup
from a in accounts
where l.Prefix == a.Prefix &&
l.Number == a.Number &&
l.Suffix == a.Suffix
select a)
).Select(a => string.Format("{0}{1}{2}", a.Prefix, a.Number, a.Suffix));
You can use the following code to get the match:
var match = (from a in accounts
select new { P = a.Prefix, N = a.Number, S = a.Suffix })
.Intersect(from l in lookup
select new { P = l.Prefix, N = l.Number, S = l.Suffix })
.Select(t => string.Format("{0}{1}{2}", t.P, t.N, t.S));;
You make use here of the automatically generated equality operators on anonymous types.
这篇关于LINQ相交或加入返回从一个集合,具有匹配性到另一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!