不同的LINQ? [英] Distinct in linq?

查看:117
本文介绍了不同的LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎么能做到这一点?

I am wondering how can I achieve this?

我想从对象集合

MyObject a = new Object();
a.Name = 'One';
a.Value = '10';


MyObject b = new Object();
b.Name = 'One';
b.Value = '15';


MyObject c = new Object();
c.Name = 'Two';
c.Value = '10';



所以我想只得到回名。我不在乎在这种情况下,仅仅是名称的值。

So I want to get only back the name. I don't care about the value in this case just the name.

所以,我想

// 。所有对象添加到集合

//add all the objects to a collection.

myCollection.Disinct()..Select(x => new MyClassToStore() {Text = x.Name, Value = x.Name}).ToList());



不过,我需要在对象级别的属性层次做不同的不行。所以,我想回去一和二​​。现在,我得到一,一和二​​了。

However I need to do distinct at the property level not at the object level. So I want back "One" and "Two". Right now I get "One", "One" and "Two" back.

我看到一个名为库的 morelinq ,但我不知道我是否应该仍处于测试阶段使用它,因为它,似乎并没有被上再发展。

I see a library called morelinq but I not sure if I should use it as it still in beta and does not seem to be developed on anymore.

另外一个提取查询全库,我不知道这是否是值得的。

Plus a whole library for one extract query I am not sure if it is worth it.

推荐答案

本填充两种文本和值字段:

This populates both the Text and Value fields:

   myCollection.Select(x => x.Name).Distinct()
      .Select(x => new MyClassToStore { Text = x, Value = x }).ToList();

这篇关于不同的LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆