score:3
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
..
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() }
});
score:0
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();
score:0
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.
what you shoud do instead is try parsing the information in an object that implements IEqualityComparer
and then use .Select(<IEqualityComparer>).Distinct()
Source: stackoverflow.com
Related Articles
- Get distinct items from DataTable using LINQ
- get distinct rows from datatable using Linq (distinct with mulitiple columns)
- Retrieve distinct values from datatable using linq vb.net
- Select Distinct from DataTable using Linq and C#
- I need to filter distinct value from datatable using IEnumerable LINQ
- Can't get distinct items from a list using LINQ
- Get Distinct data from datatable present in webservices using linq
- Fetch distinct record from datatable using linq to datatable and shows all columns of datatable
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- Select distinct values from a list using LINQ in C#
- Remove items from list that intersect on property using Linq
- Select distinct rows from datatable in Linq
- How do I remove items from generic list, based on multiple conditions and using linq
- Create Items from 3 collections using Linq
- How to get Distinct Values from List(Of T) using Linq
- Fastest way to fill DataTable from LINQ query using DataContext
- Removing sequential repeating items from List<T> using linq
- How to find duplicate record using Linq from DataTable
- Using LINQ to delete an element from a ObservableCollection Source
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- Populating a List(Of String) from a Datatable using LINQ in VB.NET
- How to generate a unique list of items from another list using LINQ in C#
- Select items from a List where the children contain the items from another List using LINQ
- Selecting distinct rows from datatable in Linq
- Get the count of distinct elements from a list of lists using LINQ
- Not getting Distinct Items when using Linq to List?
- getting same type of inherited class items from base class list using linq
- Selecting "custom distinct" items from a List using LINQ
- Using LINQ to get a list of items where the item contains a part of an item from another list
- How to efficiently select certain items from two lists using LINQ
- How to sort where an object is added to based on it's attribute
- Send a variable in where clause LINQ PHP
- Generic variadic parameters
- File Download MVC
- When does Task.WhenAll enumerate?
- Trying to get list of Id's from list of objects in MVC controller
- DefaultIfEmpty() in LINQ to SQL join causing duplicates
- LINQ query with two joins that worked in EF 6 gives error in EF 7
- Linq or string filter
- Linq to XML query... Issues with element name
- Perform "except" using particular columns
- Return exclusive elements from two lists that comes more than twice using linq
- LINQ-to-XML XElement query NULL
- Lazy Loading mvc3
- Select select query
- How to avoid composing Where() operators in LINQ?
- What XML related technologies I should know before learning LINQ to XML?
- How to split excel rows into multiple equal lots and process per iteration
- EF Creating business objects in Linq or in foreach
- Adding a large amount of records using LINQ