Linq-选择不同的值

Linq-选择不同的值,linq,distinct,Linq,Distinct,我在集合(列表)中有以下对象 这可能是长期和重复的 我想使用linq只选择vendor和unit的不同值,并将其放在以下结构中 字典 有可能吗?List l=newlist(); List<MyObject> l = new List<MyObject>(); //fill the list Dictioonary<string,string> d = l .Select ( x=>new { x.vendor,

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

这可能是长期和重复的

我想使用linq只选择vendor和unit的不同值,并将其放在以下结构中

字典

有可能吗?

List l=newlist();
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
);
//填写清单 词汇d= L .选择 ( x=>新的 { x、 供应商, x、 单位 } )//只获取您关心的属性。 .Distinct() .ToDictionary ( x=>x.vendor,//键选择器 x=>x.unit//值选择器 );
谢谢您的快速回答。通过添加更多字段(不在distinct中使用)改进了我的问题。如果distinct之后有重复的供应商,它会崩溃吗dictionary@CuongLe然后必须有更多关于如何处理该装置的信息。我们可以把它改成GroupBy,但是我们需要知道如何处理单位。但是,除非另有说明,否则解决方案是有效的。你能解释一下你字典的关键和价值吗?
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
);