我在集合中有以下对象(列表)
public class MyObject{
string vendor;
string unit;
int unit123;
AnotherObject unit456;
}
这可能很长且重复。
我想使用linq选择供应商和单位的不同值,并将其放在以下结构中
字典
是否可以?
最佳答案
List<MyObject> l = new List<MyObject>();
//fill the list
Dictioonary<string,string> d =
l
.Select
(
x=>new
{
x.vendor,
x.unit
}
) //Get only the properties you care about.
.Distinct()
.ToDictionary
(
x=>x.vendor, //Key selector
x=>x.unit //Value selector
);
关于Linq-选择不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758407/