none
linq query distinct

    Question

  •  

    Hi

    i have Employee Class

    Public Class Employee
    
    	Public Property EmpId as Integer
    
    	Public Property EmpName as String
    
    	Public Property Salary as double
    
    	Public Property DeptId as Integer
    
    End Class

    i want to get the data to ilist(of Employee) based on distinct of EmpName and DeptId

    Dim emp As IList(Of Employee) = (From entity In EmployeeList
                                     Where entity.salary = 10000
                                     Select entity.EmpName, entity.DeptId).ToList()
     

    actually i am selecting only two properties and returning to class having 4 properties 

    please help....


    Thursday, September 19, 2013 3:31 PM

Answers

  • Hi Kumar;

    This is what you need to do, create a EqualityComparer(Of Employee) to be used in removing duplicates from the results set also modify your query select clause as shown in code snippet.

    Dim emp As IList(Of Employee) = (From entity In EmployeeList
                                     Where entity.salary = 10000
                                     Select New Employee With
                                     {
                                        .EmpName = entity.EmpName,
                                        .DeptId = entity.DeptId
                                     }).Distinct(New EmployeeEqualityComparer).ToList()
    
    
    Class EmployeeEqualityComparer                                                       
        Implements IEqualityComparer(Of Employee)                                        
                                                                                                                                                                              
        Public Overloads Function Equals(ByVal e1 As Employee, ByVal e2 As Employee) _   
                       As Boolean Implements IEqualityComparer(Of Employee).Equals       
                                                                                         
            If e1.EmpName = e2.EmpName And e1.DeptId = e2.DeptId Then                    
                Return True                                                              
            Else                                                                         
                Return False                                                             
            End If                                                                       
                                                                                         
        End Function                                                                     
                                                                                         
        Public Overloads Function GetHashCode(ByVal emp As Employee) _                   
                    As Integer Implements IEqualityComparer(Of Employee).GetHashCode     
                                                                                         
            Dim hCode As Integer = emp.EmpName.GetHashCode() Xor emp.DeptId.GetHashCode()
            Return hCode                                                                 
                                                                                         
        End Function                                                                     
                                                                                         
    End Class                                                                            

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, September 20, 2013 1:25 PM

All replies

  • Hi Kumar;

    I am assuming that this is NOT aLinq To SQL but a Linq to Object type query and that EmployeeList is a collection of some object probably Employee?

    Please verify the above.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, September 19, 2013 8:34 PM
  • Hi Fernando,

    Thanks for reply....

    your are correct 

    Dim emp  = (From entity In EmployeeList
                Where entity.salary = 10000
                Select entity.EmpName, entity.DeptId).ToList()

    it is working like this

    but i is in another function and what is the return type of the function and based on the result again i have to loop 

    suppose

    for each entity as Employee in emp
        .......
        ........
    next

    how to do this....



    • Edited by Kumar Jalli Friday, September 20, 2013 9:50 AM
    Friday, September 20, 2013 6:29 AM
  • Hi Kumar;

    This is what you need to do, create a EqualityComparer(Of Employee) to be used in removing duplicates from the results set also modify your query select clause as shown in code snippet.

    Dim emp As IList(Of Employee) = (From entity In EmployeeList
                                     Where entity.salary = 10000
                                     Select New Employee With
                                     {
                                        .EmpName = entity.EmpName,
                                        .DeptId = entity.DeptId
                                     }).Distinct(New EmployeeEqualityComparer).ToList()
    
    
    Class EmployeeEqualityComparer                                                       
        Implements IEqualityComparer(Of Employee)                                        
                                                                                                                                                                              
        Public Overloads Function Equals(ByVal e1 As Employee, ByVal e2 As Employee) _   
                       As Boolean Implements IEqualityComparer(Of Employee).Equals       
                                                                                         
            If e1.EmpName = e2.EmpName And e1.DeptId = e2.DeptId Then                    
                Return True                                                              
            Else                                                                         
                Return False                                                             
            End If                                                                       
                                                                                         
        End Function                                                                     
                                                                                         
        Public Overloads Function GetHashCode(ByVal emp As Employee) _                   
                    As Integer Implements IEqualityComparer(Of Employee).GetHashCode     
                                                                                         
            Dim hCode As Integer = emp.EmpName.GetHashCode() Xor emp.DeptId.GetHashCode()
            Return hCode                                                                 
                                                                                         
        End Function                                                                     
                                                                                         
    End Class                                                                            

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, September 20, 2013 1:25 PM