Linq-选择不同的值

标签 linq distinct

我在集合中有以下对象(列表)

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/

相关文章:

sql - 选择计数(不同值)返回 1

mysql - mysql中的多列不同

sql-server - 有没有支持用nodejs写linq to sql查询的JS库?

linq - 如何在 LightSwitch 查询中使用计算字段?

c# - LINQ 选择带有 DISTINCT 的列表列表

c# List<T> 计算每一项与前一项的总和和平均值

mysql从表中选择不同的月份(缩写)

mysql - SQL 查询返回 "Operand should contain 1 column(s)"

sql - select distinct(date) 多次返回相同的日期

sql - 在 EF 中使用连接或导航属性的区别