本文介绍了转换ms sql"group by"查询到linq到sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表,产品和类别.如何将以下sql查询转换为linq格式?select c.Name, c.DisplayName, count(p.id) from categories as c
LEFT join products as p on p.categoryId = c.id
group by c.Name, c.DisplayName
某些类别中有0个产品,因此LEFT JOIN很重要
解决方案
如果在模型中正确定义了关系,那么customer表应该与所有产品都有关联,因此您将能够访问与之关联的产品客户仅使用"c.Products".然后应该能够简单地编写如下内容:
var q =
from c in categories
select new { c.Name, c.DisplayName, Count = c.Products.Count() }
T.
i have two tables, products and categories. how do i convert the following sql query to linq format?
select c.Name, c.DisplayName, count(p.id) from categories as c
LEFT join products as p on p.categoryId = c.id
group by c.Name, c.DisplayName
some categories will have 0 products in them so the LEFT JOIN is important
解决方案
if you correctly define the relations in your model then the customers table should have an association with all the products, so you'll be able to access products associated with the customer just using 'c.Products'. Then should be able to write simply something like this:
var q =
from c in categories
select new { c.Name, c.DisplayName, Count = c.Products.Count() }
T.
这篇关于转换ms sql"group by"查询到linq到sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!