zoukankan      html  css  js  c++  java
  • Exists/In/Any/All/Contains操作符

    Exists/In/Any/All/Contains操作符

    适用场景:用于判断集合中元素,进一步缩小范围。

    Any

    说明:用于判断集合中是否有元素满足某一条件;不延迟。(若条件为空,则集合只要不为空就返回True,否则为False)。有2种形式,分别为简单形式和带条件形式。

    1.简单形式:

    仅返回没有订单的客户:

    var q =    from c in db.Customers    where !c.Orders.Any()    select c;

    生成SQL语句为:

    SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName],[t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region],[t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]FROM [dbo].[Customers] AS [t0]WHERE NOT (EXISTS(    SELECT NULL AS [EMPTY] FROM [dbo].[Orders] AS [t1]    WHERE [t1].[CustomerID] = [t0].[CustomerID]   ))

    2.带条件形式:

    仅返回至少有一种产品断货的类别:

    var q =    from c in db.Categories    where c.Products.Any(p => p.Discontinued)    select c;

    生成SQL语句为:

    SELECT [t0].[CategoryID], [t0].[CategoryName], [t0].[Description],[t0].[Picture] FROM [dbo].[Categories] AS [t0]WHERE EXISTS(    SELECT NULL AS [EMPTY] FROM [dbo].[Products] AS [t1]    WHERE ([t1].[Discontinued] = 1) AND     ([t1].[CategoryID] = [t0].[CategoryID])    )

    All

    说明:用于判断集合中所有元素是否都满足某一条件;不延迟

    1.带条件形式

    var q =    from c in db.Customers    where c.Orders.All(o => o.ShipCity == c.City)    select c;

    语句描述:这个例子返回所有订单都运往其所在城市的客户或未下订单的客户。

    Contains

    说明:用于判断集合中是否包含有某一元素;不延迟。它是对两个序列进行连接操作的。

    string[] customerID_Set =    new string[] { "AROUT", "BOLID", "FISSA" };var q = (    from o in db.Orders    where customerID_Set.Contains(o.CustomerID)    select o).ToList();

    语句描述:查找"AROUT", "BOLID" 和 "FISSA" 这三个客户的订单。先定义了一个数组,在LINQ to SQL中使用Contains,数组中包含了所有的CustomerID,即返回结果中,所有的CustomerID都在这个集合内。也就是in。 你也可以把数组的定义放在LINQ to SQL语句里。比如:

    var q = (    from o in db.Orders    where (    new string[] { "AROUT", "BOLID", "FISSA" })    .Contains(o.CustomerID)    select o).ToList();

    Not Contains则取反:

    var q = (    from o in db.Orders    where !(    new string[] { "AROUT", "BOLID", "FISSA" })    .Contains(o.CustomerID)    select o).ToList();

    1.包含一个对象:

    var order = (from o in db.Orders             where o.OrderID == 10248             select o).First();var q = db.Customers.Where(p => p.Orders.Contains(order)).ToList();foreach (var cust in q){    foreach (var ord in cust.Orders)    {        //do something    }}

    语句描述:这个例子使用Contain查找哪个客户包含OrderID为10248的订单。

    2.包含多个值:

    string[] cities =     new string[] { "Seattle", "London", "Vancouver", "Paris" };var q = db.Customers.Where(p=>cities.Contains(p.City)).ToList();

    语句描述:这个例子使用Contains查找其所在城市为西雅图、伦敦、巴黎或温哥华的客户。

  • 相关阅读:
    Linux下mysql使用systemctl restart mysqld命令失败
    Linux环境下mysql报错:bash: mysql: command not found 的解决方法
    Linux查看mysql是否启动的命令
    启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting.
    ARM64架构下面安装mysql5.7.22
    Python3.6打开EAIDK-610开发板(计算机通用)摄像头拍照并保存
    Python的几种主动结束程序方式
    aarch64架构下安装tensorflow详细过程
    python代码在linux终端中执行报错:Unable to init server: Could not connect: Connection refused
    red hat 报错:apt-get:找不到命令
  • 原文地址:https://www.cnblogs.com/NotEnough/p/7423072.html
Copyright © 2011-2022 走看看