1

I have order details:

public class OrderDetails
    {
        public int OrderId { get; set; }
       // [DataMember]
        public int ProductId { get; set; }
    }

Now if I do a distinct like below , it returns everything:

List<OrderDetails> orderDetails = new List<OrderDetails>();
---------------------------------
return orderDetails.Distinct();

But if I do distinct like :

  List<OrderDetails> orderDetails = new List<OrderDetails>();
    ---------------------------------
return orderDetails.Select(x => x.OrderId).Distinct();

Then I get only Order Ids.

How can I get the Distinct Orderdetails(both OrderID and ProductId) based on OrderId

Simsons
  • 12,295
  • 42
  • 153
  • 269

5 Answers5

1

You can use the overload of Distinct which takes IEqualityComparer<TSource> as comparer.

You can define it as:

public class OrderDetailsEqualityComparer : IEqualityComparer<OrderDetails>
{
    public bool Equals(OrderDetails x, OrderDetails y)
    {
        if (object.ReferenceEquals(x, y))
        {
            return true;
        }

        if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null))
        {
            return false;
        }

        return (x.OrderId == y.OrderId );
    }

    public int GetHashCode(Product obj)
    {
        return obj.OrderId.GetHashCode();
    }
}

and use it as:

var osrderedOrderDetails =
       orderDetails.Distinct(new OrderDetailsEqualityComparer());
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

Try below code:

Distinct with OrderId

return orderDetails.GroupBy(x => x.OrderId).Select(g => g.First()).ToList();

Distinct with OrderId and ProductId

return orderDetails.GroupBy(x => new {x.OrderId, x.ProductId}).Select(g => g.First()).ToList();
Jignesh Thakker
  • 3,638
  • 2
  • 28
  • 35
  • How is this workin for you??? .Disinct expects a IEquator and you are passing a func !!! – Simsons Jan 31 '14 at 11:23
  • First GroupBy with property which you want distinct result and after that select first item of group. – Jignesh Thakker Jan 31 '14 at 11:32
  • Visit http://stackoverflow.com/questions/11811110/c-sharp-select-distinct-by-two-properties-in-a-list SO link. There is a library moreLINQ which has DistinctBy method. – Jignesh Thakker Feb 01 '14 at 17:02
0

You could create a method that takes an Order Id, and return the list as below...

public List<OrderDetails> GetDistinctOrderDetails(int orderId)
{
  List<OrderDetails> orderDetails = GetAllOrders();
  return orderDetails.Where(x => x.OrderId == orderId).Distinct();
}
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
0
return orderdetails.Where(a=>a.OrderID == _inputvalue).ToArray();

"toarray" can be any of the collection types what ever you need unless your function returns Ienumerable<> then you don't need it

using distinct() is not a good idea as it will drop rows

RadioSpace
  • 953
  • 7
  • 15
0

Implement IEquatable interface:

public class OrderDetails : IEquatable<OrderDetails>
{
    public int OrderId { get; set; }

    public int ProductId { get; set; }

    public bool Equals(OrderDetails anotherOrder)
    {
        return this.OrderId.Equals(anotherOrder.OrderId);
    }

    public override int GetHashCode()
    {
        return this.OrderId;
    }
}

Now, try orderDetails.Distinct();

Samina Azad
  • 51
  • 1
  • 4