C# 仅基于表的一个字段在 Linq 中不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14321013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:25:51  来源:igfitidea点击:

Distinct in Linq based on only one field of the table

c#sqllinq

提问by Megha Jain

I am trying to use .distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table).

我试图在 Linq 中使用 .distinct 根据表的一个字段获取结果(因此不需要表中的整个重复记录)。

I know writing basic query using distinct as followed:

我知道使用 distinct 编写基本查询如下:

var query = (from r in table1
orderby r.Text
select r).distinct();

but I need results where r.textis not duplicated.

但我需要r.text不重复的结果。

采纳答案by Daniel Hilgarth

Try this:

尝试这个:

table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());

This will group the table by Textand use the first row from each groups resulting in rows where Textis distinct.

这将对表格进行分组Text并使用每个组中的第一行,从而产生Text不同的行。

回答by Tim Schmelter

but I need results where r.text is not duplicated

但我需要 r.text 不重复的结果

Sounds as if you want this:

听起来好像你想要这个:

table1.GroupBy(x => x.Text)
      .Where(g => g.Count() == 1)
      .Select(g => g.First());

This will select rows where the Textis unique.

这将选择Text唯一的行。

回答by Servy

MoreLinq has a DistinctBymethod that you can use:

MoreLinq 有一个DistinctBy方法,您可以使用:

It will allow you to do:

它将允许您执行以下操作:

var results = table1.DistictBy(row => row.Text);

The implementation of the method (short of argument validation) is as follows:

该方法的实现(缺少参数验证)如下:

private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
    HashSet<TKey> knownKeys = new HashSet<TKey>(comparer);
    foreach (TSource element in source)
    {
        if (knownKeys.Add(keySelector(element)))
        {
            yield return element;
        }
    }
}

回答by TKharaishvili

There are lots of discussions around this topic.

关于这个话题有很多讨论。

You can find one of them here:

你可以在这里找到其中之一:

One of the most popular suggestions have been the Distinct method taking a lambda expression as a parameter as @Servy has pointed out.

正如@Servy 指出的那样,最受欢迎的建议之一是 Distinct 方法将 lambda 表达式作为参数。

The chief architect of C#, Anders Hejlsberg has suggested the solution here. Also explaining why the framework design team decided not to add an overload of Distinct method which takes a lambda.

C# 的首席架构师 Anders Hejlsberg 在这里提出了解决方案。还解释了为什么框架设计团队决定不添加采用 lambda 的 Distinct 方法的重载。

回答by LucaGuerra

You can try this:table1.GroupBy(t => t.Text).Select(shape => shape.r)).Distinct();

你可以试试这个:table1.GroupBy(t => t.Text).Select(shape => shape.r)).Distinct();

回答by Josh Parks

From what I have found, your query is mostly correct. Just change "select r" to "select r.Text" is all and that should solve the problem. This is how MSDN documented how it should work.

根据我的发现,您的查询大部分是正确的。只需将“select r”更改为“select r.Text”即可解决问题。这就是 MSDN 记录它应该如何工作的方式。

Ex:

前任:

    var query = (from r in table1 orderby r.Text select r.Text).distinct();

回答by Biraj Saha

Daniel Hilgarth's answer above leads to a System.NotSupportedexception With Entity-Framework. With Entity-Framework, it has to be:

上面 Daniel Hilgarth的回答导致了Entity-FrameworkSystem.NotSupported异常。使用Entity-Framework,它必须是:

table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());

回答by bgS

data.Select(x=>x.Name).Distinct().Select(x => new SelectListItem { Text = x });

回答by HamidReza

try this code :

试试这个代码:

table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());