Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
i want to get the distinct EmployeeId for the following query. I used Distinct but its not working
var emp= (from dr in dt.AsEnumerable() orderby dr["EmployeeID"] descending
         select new Employee(){
EmployeeId = int.Parse(dr"EmployeeID"].ToString()),     
EmployeeName = dr["EmployeeName"].ToString(),
EmployeeDesignation = dr["EmployeeDesignation"].ToString()
         }).Distinct();
Posted
Updated 24-Oct-12 21:23pm
v2

Since you have added the "." and it's still not working, I am guessing that your class does not implement IEquatable.
What that means is that Distinct will use the default comparer, which for a class is a Reference comparison. Since all your instances are new instances, they will all fail a reference comparison, because the references are not the same.

You need to either implement IEquatable<Employee> and provide the Equals method (at a guess comparing EmployeeID values) and the GetHashCode method, or provide a custom comparer to the Distinct call: http://msdn.microsoft.com/en-us/library/bb338049.aspx[^] describes it and provides an example.
 
Share this answer
 
You need a '.' before the word "Distinct":
C#
var emp= (from dr in dt.AsEnumerable() orderby dr["EmployeeID"] descending
         select new Employee(){
EmployeeId = int.Parse(dr"EmployeeID"].ToString()),
EmployeeName = dr["EmployeeName"].ToString(),
EmployeeDesignation = dr["EmployeeDesignation"].ToString()
         })Distinct();
Becomes:
C#
var emp= (from dr in dt.AsEnumerable() orderby dr["EmployeeID"] descending
         select new Employee(){
EmployeeId = int.Parse(dr"EmployeeID"].ToString()),
EmployeeName = dr["EmployeeName"].ToString(),
EmployeeDesignation = dr["EmployeeDesignation"].ToString()
         }).Distinct();
 
Share this answer
 
Comments
Nivas Maran 25-Oct-12 3:24am    
i used . before .Distinct() but its not working yet
OriginalGriff 25-Oct-12 3:31am    
"it's not working" is not very helpful - what does it do that it shouldn't, or not do that it should?
Why it will work.

C#
var emp= (from dr in dt.AsEnumerable() orderby dr["EmployeeID"] descending
         select new Employee(){
EmployeeId = int.Parse(dr"EmployeeID"].ToString()),     
EmployeeName = dr["EmployeeName"].ToString(),
EmployeeDesignation = dr["EmployeeDesignation"].ToString()
         }).Distinct();
 
Share this answer
 
 
Share this answer
 
C#
var emp= (from dr in dt.AsEnumerable() orderby dr["EmployeeID"] descending
         select new Employee(){
EmployeeId = int.Parse(dr"EmployeeID"].ToString()),
EmployeeName = dr["EmployeeName"].ToString(),
EmployeeDesignation = dr["EmployeeDesignation"].ToString()
         })Distinct();



Solution 1.

i think you missed . operater in your query , make it .Distinct()


or

SOlution 2.

another way is use a stored procedure and then add that SP to your DBML. It will create a simple function that you can then call through LINQ.
 
Share this answer
 
Try this
EmployeeId = int.Parse(dr["EmployeeID]"])

or
EmployeeId = int.Parse(dr["EmployeeID"].ToString())

instead of
EmployeeId = int.Parse(dr"EmployeeID"].ToString())
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900