.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

LINQ Distinct not work

Posted By: Narayanan     Posted Date: December 26, 2012    Points:5   Category :C#
List<ClsEmployees> objEmployee = new List<ClsEmployees>
{
new ClsEmployees { EmpID = "1001" ,Name ="GANTEC", Salary =10000 },
new ClsEmployees { EmpID = "1002" ,Name ="IBM", Salary =15000 },
new ClsEmployees { EmpID = "1003" ,Name ="CTS", Salary =20000 },
new ClsEmployees { EmpID = "1004" ,Name ="WIPRO", Salary =30000 },
new ClsEmployees { EmpID = "1005" ,Name ="SATHYAM", Salary =40000 },
new ClsEmployees { EmpID = "1006" ,Name ="BUTTERFLY", Salary =50000 },
new ClsEmployees { EmpID = "1007" ,Name ="CON", Salary =10000 },
new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 },
new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 },
new ClsEmployees { EmpID = "1010" ,Name ="TATA", Salary =90000 },
new ClsEmployees { EmpID = "1009" ,Name ="GANTEC COR", Salary =100000 },
new ClsEmployees { EmpID = "1009" ,Name ="GANTEC COR", Salary =100000 },
};

Query for Distinct:
var dis = (from emp in objEmployee select emp).Distinct();
dataGridView1.DataSource = dis.ToList();

It doesn't work .Why?

Responses
Author: Petr Matejovsky             
Posted Date: December 26, 2012     Points: 20   

You might need an AsEnumerable()

Try this code :

List<ClsEmployees> objEmployee = new List<ClsEmployees>
{
new ClsEmployees { EmpID = "1001" ,Name ="GANTEC", Salary =10000 },
new ClsEmployees { EmpID = "1002" ,Name ="IBM", Salary =15000 },
new ClsEmployees { EmpID = "1003" ,Name ="CTS", Salary =20000 },
new ClsEmployees { EmpID = "1004" ,Name ="WIPRO", Salary =30000 },
new ClsEmployees { EmpID = "1005" ,Name ="SATHYAM", Salary =40000 },
new ClsEmployees { EmpID = "1006" ,Name ="BUTTERFLY", Salary =50000 },
new ClsEmployees { EmpID = "1007" ,Name ="CON", Salary =10000 },
new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 },
new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 },
new ClsEmployees { EmpID = "1010" ,Name ="TATA", Salary =90000 },
new ClsEmployees { EmpID = "1009" ,Name ="GANTEC COR", Salary =100000 },
new ClsEmployees { EmpID = "1009" ,Name ="GANTEC COR", Salary =100000 },
};


var dis = (from emp in objEmployee.AsEnumerable()
select new { emp.EmpID , emp.Name , emp.Salary }).Distinct();

dataGridView1.DataSource = dis.ToList();


Petr

Author: Narayanan             
Posted Date: December 26, 2012     Points: 20   

Hi ,
Thanks

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend