Question

There are two linq querys in below, they return complete difference result, first query return 4 records second return 72 records. I think they are same. Who can explain why they return difference record set. Thanks for helping.

void Main()
{
    var q1 = from c in Customers
            where !c.Orders.Any(o => o.OrderDetails.Sum(od => od.UnitPrice * od.Quantity) < 1000)
            select new {c.CustomerID, c.ContactName};

    q1.Dump();

    var q2 = from c in Customers
             where c.Orders.Any(o => o.OrderDetails.Sum(od => od.Quantity * od.UnitPrice) >= 1000)
             select new {c.CustomerID, c.ContactName};

    q2.Dump();
}
Was it helpful?

Solution

  1. The first returns customers that do not have any orders whose total price is less than 1000.
  2. The second returns customers that have any orders whose total price is greater than or equal to 1000.

In other words, imagine a customer who made one order of total price less than 1000, and another order of total price greater than 1000. Because of the smaller order, the customer record would not be included in the first result set, but because of the larger order, it would be included in the second result set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top