简体   繁体   English

使用Linq区分行

[英]Distinct rows using Linq

I have been trying to get this query to work from a long time! 我一直在尝试使此查询从很长时间开始工作!
Table: 表:
ID - Base - Date ID-基本-日期
1 - 1991 - 2013-09-13 1-1991-2013-09-13
2 - 1992 - 2013-01-12 2-1992-2013-01-12
3 - 1990 - 2013-09-13 3-1990-2013-09-13
4 - 1998 - 2013-02-11 4-1998-2013-02-11

I want to get all the rows with distinct Dates. 我想获取具有不同日期的所有行。 For eg: 例如:
Required Output 所需输出
ID - Base - Date ID-基本-日期
1 - 1991 - 2013-09-13 1-1991-2013-09-13
2 - 1992 - 2013-01-12 2-1992-2013-01-12
4 - 1998 - 2013-02-11 4-1998-2013-02-11

What i have so far is displaying all the records and thats not what I want. 到目前为止,我正在显示所有记录,而那不是我想要的。
My Code: 我的代码:

public IEnumerable<tableName> GetAllRecords()
    {
        var records = _tableName.GetAll().GroupBy(tb => tb.Date).SelectMany(tb => tb.Distinct());

        return records;
    }    

How do I get the required output? 如何获得所需的输出?

public IEnumerable<tableName> GetAllRecords() {
    var records = _tableName.GetAll()
                  .GroupBy(tb => tb.Date)
                  .Select(g=>g.First());
    return records;
}  

NOTE : it's still unclear on what exactly what you want, we have the Base and ID parts of duplicated rows are different , so you may have some criteria or rule to choose between them in a group of Date . :这是什么你想要什么,我们有还不清楚BaseID重复的行的部分是不同的 ,所以你可能有一些标准或规则中的一组他们之间做出选择Date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM