LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains

看了李永京写的LINQ。 现试了一次。 现在只记下自已已测试过的。
Group By/Having操作符
var q =
    from p in db.Products
    group p by p.CategoryID into g
    select g;
有人会说Having呢。 其实我也不知道怎么用。 试了几次都没有成功。

最大值

说明:先按CategoryID归类,判断各个分类产品中单价最大的Products。取出CategoryID值,并把UnitPrice值赋给MaxPrice。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
MaxPrice = g.Max(p => p.UnitPrice)
};
Min,Average,Sum   唯独Count不同。它要写成如此形式
var q =
    from p in db.Products
    group p by p.CategoryID into g
    select new {
        g.Key,
        NumProducts = g.Count()
    };

8.带条件计数

说明:先按CategoryID归类,取出CategoryID值和各个分类产品的断货数量。 Count函数里,使用了Lambda表达式,Lambda表达式中的p,代表这个组里的一个元素或对象,即某一个产品。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
NumProducts = g.Count(p => p.Discontinued)
};
其实这个功能不太好。主要是Count()这里面只能放布尔值的数据。


9.Where限制

说明:在翻译成SQL语句时,在最外层嵌套了Where条件。

var q =
from p in db.Products
group p by p.CategoryID into g
where g.Count() >= 10     //这里好像只能放Count.如果我Max(p.ID)就会有错。
select new {
g.Key,
ProductCount = g.Count()
};

语句描述:根据产品的―ID分组,查询产品数量大于10的ID和产品数量。


这个功能也相当的不好。 很局限。


10.Multiple Columns

说明:既按产品的分类,又按供应商分类。在by后面,new出来一个匿名类。这里,Key其实质是一个类的对象,Key包含两个Property:CategoryID、SupplierID。用g.Key.CategoryID可以遍历CategoryID的值。

var categories =
from p in db.Products
group p by new
{
p.CategoryID,
p.SupplierID
}
into g
select new
{
g.Key,
g
};

11.Expression(表达式)

说明:按产品单价是否大于10分类。其结果分为两类,大于的是一类,小于及等于为另一类。

var categories =
from p in db.Products
group p by new { Criterion = p.UnitPrice > 10 } into g
select g;
 
 

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)  //这里要注意  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])
)
 
包含多个值:
string[] cities =
new string[] { "Seattle", "London", "Vancouver", "Paris" };
var q = db.Customers.Where(p=>cities.Contains(p.City)).ToList();

转载于:https://www.cnblogs.com/z2002m/archive/2008/07/09/1239139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值