C# 使用 LINQ 从 DataTable 中获取不同的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9299223/
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-09 06:46:59  来源:igfitidea点击:

Get distinct items from DataTable using LINQ

c#asp.netlinq

提问by Steven

I have data being returned in a DataTable that looks like:

我在 DataTable 中返回了数据,如下所示:

Department | EmployeeName

1 | Employee1

1 | Employee1

2 | Employee2

3 | Employee3

3 | Employee3

3 | Employee3

部门 | 员工姓名

1 | 员工1

1 | 员工1

2 | 员工2

3 | 员工3

3 | 员工3

3 | 员工3

I'm trying to get only distinct rows and put it into a collection, like so:

我试图只获取不同的行并将其放入一个集合中,如下所示:

IEnumerable<Department> departments = dt.AsEnumerable().Select(row =>
    new Department
        {
            DepartmentID = Int32.Parse(row["DepartmentID"].ToString()),
            Employee = new Employee { EmployeeName = row["EmployeeName"].ToString() }
        }).Distinct();

It should only return 3 rows, but instead it's returning all 6.

它应该只返回 3 行,而是返回所有 6 行。

What am I doing wrong here?

我在这里做错了什么?

采纳答案by Pranay Rana

Not sure but one of the following may work for you ..........

不确定,但以下方法之一可能适合您.........

just do this

就这样做

var distrows= table1.AsEnumerable().Distinct();

will do your task..........than create collection of department form the distrow..

将完成你的任务.......比创建部门的集合distrow......

IEnumerable<Department> departments = (from DataRow dRow in distrows   
                         new Department
        {
            DepartmentID = Int32.Parse(row["DepartmentID"].ToString()),
            Employee = new Employee { EmployeeName = row["EmployeeName"].ToString() }
        });

OR

或者

var distinctRows = (from DataRow dRow in dTable.Rows
            select new {col1=dRow["dataColumn1"],col2=dRow["dataColumn2"]}).Distinct();


IEnumerable<Department> departments = (from DataRow dRow in distrows   
                             new Department
            {
                DepartmentID = Int32.Parse(distinctRows.col1),
                Employee = new Employee { EmployeeName = distinctRows.col2.ToString() }
            });

回答by Pankaj

Class/Schema

类/模式

public class abc
{
    public int id;
}


DataTable and Records

数据表和记录

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id", Type.GetType("System.Int32"));
dt.Columns.Add(dc);

DataRow dr = dt.NewRow();
dr["id"] = 1;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["id"] = 1;
dt.Rows.Add(dr);


Query

询问

var qa = dt.AsEnumerable()
.Select(row => new
{
    Id = row.Field<int>("id"),
})
.Distinct();


回答by Parv Sharma

what i think the problem here is that you are creating different objects with have no implementation for comparing the equality based on some propertiese. so the Distinct()method compares the refference of the objects and hence concludes that all the objects are different because they point to different reffrences.

我认为这里的问题是您正在创建不同的对象,但没有实现基于某些属性来比较相等性的实现。因此该Distinct()方法比较对象的引用,因此得出所有对象都不同的结论,因为它们指向不同的引用。

what you shoud do instead is try parsing the information in an object that implements IEqualityComparerand then use .Select(<IEqualityComparer>).Distinct()

你应该做的是尝试解析一个对象中的信息,该对象实现IEqualityComparer然后使用.Select(<IEqualityComparer>).Distinct()