user1227851
user1227851

Reputation: 21

I want a distinct or group by data from this query

this is my query.

var query = from o in context.Orders
            join c in context.Customers on
                o.CustomerId equals c.CustomerId  
            join ca in context.CustomerAttributes on
                o.CustomerId equals ca.CustomerId
            where (String.IsNullOrEmpty(customerEmail) ||
                      c.Email.Contains(customerEmail)) &&
                  (String.IsNullOrEmpty(firstName) ||
                      (ca.Key.Contains("FirstName") &&
                       ca.Value.Contains(firstName))) &&
                  (String.IsNullOrEmpty(lastName) ||
                      (ca.Key.Contains("LastName") &&
                      ca.Value.Contains(lastName))) &&
                  (String.IsNullOrEmpty(phoneNumber) ||
                      (ca.Key.Contains("PhoneNumber") && 
                      ca.Value.Contains(phoneNumber))) &&
                  (!startTime.HasValue || 
                      startTime.Value <= o.CreatedOn) &&
                  (!endTime.HasValue ||
                      endTime.Value >= o.CreatedOn) &&
                  (!orderStatusId.HasValue ||
                      orderStatusId == o.OrderStatusId) &&
                  (!paymentStatusId.HasValue ||
                      paymentStatusId.Value == o.PaymentStatusId) &&
                  (!shippingStatusId.HasValue ||
                      shippingStatusId.Value == o.ShippingStatusId) &&
                  !o.Deleted
            orderby o.CreatedOn descending
            select o;

var orders = query.ToList();
return orders;

i have got repetition data from this query. now i want only the distinct data from this query. any solution would be appreciable.

Upvotes: 2

Views: 145

Answers (1)

Marcel B
Marcel B

Reputation: 3664

Just use the Distinct Function

var orders = query.Distinct().ToList();

Upvotes: 2

Related Questions