none
Distinct RRS feed

  • Question

  • Hello,

    i have a table called "Season" , and i would like to return all season distinct by the name of the season

    all what i can do is this :

    using (DAO.tophajjwebEntities ctx = new DAO.tophajjwebEntities())
             {

                   var result = (ctx.SAISONs).distinct();

                 return result.ToBOSaisons();

            }

    i don't know where i shoukd make the condition to do the distinct only for the "Name" column of the table season

    thx in advance

    Tuesday, November 19, 2013 4:26 PM

Answers

  • Hello,

    Since I do not know the table structure, I use my table to do a sample and please have a look at the sample below:

    First we need to create a custom class which implements interface IEqualityComparer<TSource> like below:

    public class OrderDetailComparer : IEqualityComparer<OrderDetail>
    
        {
    
            public bool Equals(OrderDetail x, OrderDetail y)
    
            {
    
                return x.OrderDetailName == y.OrderDetailName;
    
            }
    
    
            public int GetHashCode(OrderDetail obj)
    
            {
    
                return obj.OrderDetailName.GetHashCode();
    
            }
    
        }
    

    Using lambda expression:

    var result = db.OrderDetails.ToList().Distinct(new OrderDetailComparer()); 

    Using linq query:

    var result = (from od in db.OrderDetails
    
                                  select od).ToList().Distinct(new OrderDetailComparer()); 
    

    It will return a collection distinct by ‘OrderDetailName’.

    For the reason to use the ToList(), because Distinct() with the comparer can't be translated to SQL by EF.

    However, I recommend you to use the grouping in Entity Framework instead of distinct.

    For why, please have a look this article.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by hishwest Wednesday, November 20, 2013 8:23 AM
    Wednesday, November 20, 2013 4:30 AM
    Moderator

All replies

  • Hello,

    Since I do not know the table structure, I use my table to do a sample and please have a look at the sample below:

    First we need to create a custom class which implements interface IEqualityComparer<TSource> like below:

    public class OrderDetailComparer : IEqualityComparer<OrderDetail>
    
        {
    
            public bool Equals(OrderDetail x, OrderDetail y)
    
            {
    
                return x.OrderDetailName == y.OrderDetailName;
    
            }
    
    
            public int GetHashCode(OrderDetail obj)
    
            {
    
                return obj.OrderDetailName.GetHashCode();
    
            }
    
        }
    

    Using lambda expression:

    var result = db.OrderDetails.ToList().Distinct(new OrderDetailComparer()); 

    Using linq query:

    var result = (from od in db.OrderDetails
    
                                  select od).ToList().Distinct(new OrderDetailComparer()); 
    

    It will return a collection distinct by ‘OrderDetailName’.

    For the reason to use the ToList(), because Distinct() with the comparer can't be translated to SQL by EF.

    However, I recommend you to use the grouping in Entity Framework instead of distinct.

    For why, please have a look this article.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by hishwest Wednesday, November 20, 2013 8:23 AM
    Wednesday, November 20, 2013 4:30 AM
    Moderator
  • thank you  very much , it works :)
    Wednesday, November 20, 2013 8:24 AM