Linq查询以获取列表中的不同值

时间:2014-01-02 13:18:09

标签: c# linq c#-4.0

假设这是我的会员类

class Member 
{
    public string CategoryId { get; set; }
    public string MemberName { get; set; }
    public int Distance { get; set; }
}

而且,这是列表。

var list = new List<Member>();
list.Add(new { CategoryId = "01", MemberName="andy" Distance=3});
list.Add(new { CategoryId = "02", MemberName="john" Distance=5});
list.Add(new { CategoryId = "01", MemberName="mathew" Distance=7});
list.Add(new { CategoryId = "03", MemberName="bakara" Distance=2});

任何人都可以建议使用逻辑/ linq查询来获得具有最短距离的不同/唯一categoryID的List。

output应为:

list.Add(new { CategoryId = "01", MemberName="andy" Distance=3});
list.Add(new { CategoryId = "02", MemberName="john" Distance=5});
list.Add(new { CategoryId = "03", MemberName="bakara" Distance=2});

7 个答案:

答案 0 :(得分:22)

这应该可以满足您的需求:

var grouped = list.GroupBy(item => item.CategoryId);
var shortest = grouped.Select(grp => grp.OrderBy(item => item.Distance).First());

首先将具有相同CategoryId的项目分组,然后从每个组中选择第一个(按Distance排序)。

答案 1 :(得分:4)

这与上面的相似,但从技术上来说,对于那些感兴趣的人来说,它是一个衬垫......

var queryResult= (from l in list
                  select new Member()
                 { 
                    CategoryId = l.CategoryId , 
                    MemberName = l.MemberName 
                    Distance = l.Distance
                 }).GroupBy(x=>x.CatagoryId).Select(z=>z.OrderBy(i=>i.Distance).First()).ToList();

答案 2 :(得分:1)

您可以使用以下代码:

List<Member> sourceList = new List<Member>();
IEnumerable<Member> result = 
    (sourceList as IEnumerable<Member>)
    .Distinct()
    .OrderBy(value => value.CategoryId);

答案 3 :(得分:1)

将列表分组,然后按距离对每个分组进行排序,取第一项(最低距离)。将结果投影到新的Member集合中。

var query = from member in list 
            group member by member.CategoryId into memberGrouping
            let groupedMember = memberGrouping.OrderBy (mg => mg.Distance).First() 
            select new Member()
            { 
              CategoryId = memberGrouping.Key, 
              MemberName = groupedMember.MemberName, 
              Distance = groupedMember.Distance 
            };

答案 4 :(得分:0)

试试这个

var distinctIds = list.Distinct(item => item.CategoryId).ToList();
var newList = new List<Member>();
foreach(var id in distinctIds){
      newList.Add(list.Where(item => item.CategoryId == id).Min(item => item.Distance))
}
newList.OrderBy(item => item.CategoryId);

答案 5 :(得分:0)

class Member {
    public string CategoryId {get; set;}
    public string MemberName{get; set;}
    public int Distance{get; set;}
}

var list = new List<Member>();
list.Add(new Member{ CategoryId = "01", MemberName="andy", Distance=3});
list.Add(new Member{ CategoryId = "02", MemberName="john", Distance=5});
list.Add(new Member{ CategoryId = "01", MemberName="mathew", Distance=7});
list.Add(new Member{ CategoryId = "03", MemberName="bakara", Distance=2});

var query = list.GroupBy(member => member.CategoryId).Select(x=>x.OrderBy(y=>y.Distance).First());

答案 6 :(得分:0)

如果您不需要(少量修改Kjartan's answer

订购的商品,这也可以
var grouped = list.GroupBy(item => item.CategoryId).ToList();
var shortest = grouped.Select(grp => grp.OrderBy(item => item.Distance).First());