我必须执行以下SQL查询:
select answer_nbr, count(distinct user_nbr)
from tpoll_answer
where poll_nbr = 16
group by answer_nbr
LINQ to SQL查询
from a in tpoll_answer
where a.poll_nbr = 16 select a.answer_nbr, a.user_nbr distinct
映射到以下SQL查询:
select distinct answer_nbr, distinct user_nbr
from tpoll_answer
where poll_nbr = 16
到目前为止,这么好。然而,当尝试对结果进行GROUP时,问题就出现了,因为我无法找到映射到我在这里写的第一个查询的LINQ to SQL查询(感谢LINQPad使这个过程变得更容易) 。以下是我发现的唯一能给我预期效果的结果:
from answer in tpoll_answer where answer.poll_nbr = 16 _
group by a_id = answer.answer_nbr into votes = count(answer.user_nbr)
在所有SQL查询中,这反过来会产生丑陋和非优化:
SELECT [t1].[answer_nbr] AS [a_id], (
SELECT COUNT(*)
FROM (
SELECT CONVERT(Bit,[t2].[user_nbr]) AS [value], [t2].[answer_nbr], [t2].[poll_nbr]
FROM [TPOLL_ANSWER] AS [t2]
) AS [t3]
WHERE ([t3].[value] = 1) AND ([t1].[answer_nbr] = [t3].[answer_nbr]) AND ([t3].[poll_nbr] = @p0)
) AS [votes]
FROM (
SELECT [t0].[answer_nbr]
FROM [TPOLL_ANSWER] AS [t0]
WHERE [t0].[poll_nbr] = @p0
GROUP BY [t0].[answer_nbr]
) AS [t1]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [16]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
任何帮助都将得到更多的赞赏。
答案 0 :(得分:87)
没有对COUNT(DISTINCT {x}))
的直接支持,但您可以从IGrouping<,>
模拟它(即group by
返回的内容);我担心我只会“做”C#,所以你必须翻译成VB ......
select new
{
Foo= grp.Key,
Bar= grp.Select(x => x.SomeField).Distinct().Count()
};
这是Northwind示例:
using(var ctx = new DataClasses1DataContext())
{
ctx.Log = Console.Out; // log TSQL to console
var qry = from cust in ctx.Customers
where cust.CustomerID != ""
group cust by cust.Country
into grp
select new
{
Country = grp.Key,
Count = grp.Select(x => x.City).Distinct().Count()
};
foreach(var row in qry.OrderBy(x=>x.Country))
{
Console.WriteLine("{0}: {1}", row.Country, row.Count);
}
}
TSQL并不是我们想要的,但它确实起到了作用:
SELECT [t1].[Country], (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [t2].[City]
FROM [dbo].[Customers] AS [t2]
WHERE ((([t1].[Country] IS NULL) AND ([t2].[Country] IS NULL)) OR (([t1]
.[Country] IS NOT NULL) AND ([t2].[Country] IS NOT NULL) AND ([t1].[Country] = [
t2].[Country]))) AND ([t2].[CustomerID] <> @p0)
) AS [t3]
) AS [Count]
FROM (
SELECT [t0].[Country]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[CustomerID] <> @p0
GROUP BY [t0].[Country]
) AS [t1]
-- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
然而,通过手动运行结果是正确的:
const string sql = @"
SELECT c.Country, COUNT(DISTINCT c.City) AS [Count]
FROM Customers c
WHERE c.CustomerID != ''
GROUP BY c.Country
ORDER BY c.Country";
var qry2 = ctx.ExecuteQuery<QueryResult>(sql);
foreach(var row in qry2)
{
Console.WriteLine("{0}: {1}", row.Country, row.Count);
}
定义:
class QueryResult
{
public string Country { get; set; }
public int Count { get; set; }
}
答案 1 :(得分:10)
Marc Gravell引用的Northwind示例可以使用由群组声明直接选择的City列重写:
from cust in ctx.Customers
where cust.CustomerID != ""
group cust.City /*here*/ by cust.Country
into grp
select new
{
Country = grp.Key,
Count = grp.Distinct().Count()
};
答案 2 :(得分:1)
Linq to sql不支持Count(Distinct ...)。因此,您必须将代码中的.NET方法映射到Sql服务器函数(因此为Count(distinct ..))并使用它。
顺便说一下,如果你发布从工具箱中复制的伪代码,它既不是VB.NET也不是C#的格式,它无济于事。答案 3 :(得分:1)
这是您执行非常计数查询的方式。请注意,您必须过滤掉空值。
var useranswercount = (from a in tpoll_answer
where user_nbr != null && answer_nbr != null
select user_nbr).Distinct().Count();
如果将此与您当前的分组代码结合使用,我认为您将获得解决方案。
答案 4 :(得分:1)
在LINQ中如何工作的简单而干净的示例
http://www.a2zmenu.com/LINQ/LINQ-to-SQL-Group-By-Operator.aspx
答案 5 :(得分:-4)
我不想在Linq2SQL中做这件事。为您想要和理解的查询创建存储过程,然后在框架中创建存储过程的对象,或者直接连接到它。