简体   繁体   English

这两个linq查询之间有什么区别?

[英]What is difference between these two linq queries?

There are two linq querys in below, they return complete difference result, first query return 4 records second return 72 records. 下面有两个linq查询,它们返回完整的差值结果,第一个查询返回4条记录,第二个返回72条记录。 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();
}
  1. The first returns customers that do not have any orders whose total price is less than 1000. 第一个返回的客户没有任何总价格低于 1000的订单。
  2. The second returns customers that have any orders whose total price is greater than or equal to 1000. 第二返回任何订单的总价格高于或等于 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. 换句话说,假设有一个客户的一个订单的总价格小于1000,而另一个订单的总价格大于1000。由于订单量较小,该客户记录将不包括在第一个结果集中,而是较大的顺序包含在第二个结果集中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM