Everything I can find in linq for aggregation has a "group by" clause. How would I write this query in LINQ? I have a list of date-value pairs, and I want to take the average of the values:
我在linq中可以找到的所有聚合都有一个“group by”子句。我如何在LINQ中编写此查询?我有一个日期 - 值对列表,我想取平均值:
SELECT AVG(MySuff.Value) AS AvgValue FROM MyStuff
6 个解决方案
#1
1
The answer to modified example (I believe) is:
修改后的例子(我相信)的答案是:
var average = (from a in MyStuff
select a.Value).Average();
#2
4
morning Alan:
int count = (from a in myContext.MyStuff
select a).Count();
Assuming myContext is the DataContext.
假设myContext是DataContext。
Note that is gives you immediate execution, which you may not want.
请注意,这可以让您立即执行,这可能是您不想要的。
You could instead store the results of the query in a var:
您可以将查询结果存储在var中:
var allResults = from a in myContext.MyStuff
select a;
//sometime later when needed
int count = allResults.Count(); // executes the SQL query now!
#3
2
There are plenty of non-grouping aggregation operators in LINQ. Alan's answer shows you the Count
operator, but MSDN lists others.
LINQ中有很多非分组聚合运算符。 Alan的回答显示了Count运算符,但MSDN列出了其他运算符。
EDIT: Having seen your edit, it looks like you want the Average operator.
编辑:看过您的编辑后,看起来您想要平均值运算符。
#4
1
It should be noted that Alan's LINQ code will yield exactly AlanR's SQL code, despite the fact that you might guess otherwise.
应该注意的是,Alan的LINQ代码将准确地产生AlanR的SQL代码,尽管你可能会猜测。
However, care should be exercised here. If it were written as:
但是,应该在这里谨慎行事。如果它写成:
var q = from a in MyStuff select a;
int count = q.count();
foreach(MyStuff m in q) {...}
Then that will generate two DB queries : the first as "select count(*)..." and the second as "select * ...."
然后,这将生成两个DB查询:第一个作为“select count(*)...”,第二个作为“select * ....”
#5
1
A bit sorter
有点分拣机
pairs.Average(a=>a.Value)
If there's no join, group or let, Query Expressions (from ...) are not worth in my opinion.
如果没有联接,分组或让,查询表达式(来自...)在我看来是不值得的。
#6
0
Thank you all for the help. Here is what I settled on, which works.
谢谢大家的帮助。这就是我所确定的,有效的。
(from s in series select s).Average( a => a.Value )
Regards, Alan...R
#1
1
The answer to modified example (I believe) is:
修改后的例子(我相信)的答案是:
var average = (from a in MyStuff
select a.Value).Average();
#2
4
morning Alan:
int count = (from a in myContext.MyStuff
select a).Count();
Assuming myContext is the DataContext.
假设myContext是DataContext。
Note that is gives you immediate execution, which you may not want.
请注意,这可以让您立即执行,这可能是您不想要的。
You could instead store the results of the query in a var:
您可以将查询结果存储在var中:
var allResults = from a in myContext.MyStuff
select a;
//sometime later when needed
int count = allResults.Count(); // executes the SQL query now!
#3
2
There are plenty of non-grouping aggregation operators in LINQ. Alan's answer shows you the Count
operator, but MSDN lists others.
LINQ中有很多非分组聚合运算符。 Alan的回答显示了Count运算符,但MSDN列出了其他运算符。
EDIT: Having seen your edit, it looks like you want the Average operator.
编辑:看过您的编辑后,看起来您想要平均值运算符。
#4
1
It should be noted that Alan's LINQ code will yield exactly AlanR's SQL code, despite the fact that you might guess otherwise.
应该注意的是,Alan的LINQ代码将准确地产生AlanR的SQL代码,尽管你可能会猜测。
However, care should be exercised here. If it were written as:
但是,应该在这里谨慎行事。如果它写成:
var q = from a in MyStuff select a;
int count = q.count();
foreach(MyStuff m in q) {...}
Then that will generate two DB queries : the first as "select count(*)..." and the second as "select * ...."
然后,这将生成两个DB查询:第一个作为“select count(*)...”,第二个作为“select * ....”
#5
1
A bit sorter
有点分拣机
pairs.Average(a=>a.Value)
If there's no join, group or let, Query Expressions (from ...) are not worth in my opinion.
如果没有联接,分组或让,查询表达式(来自...)在我看来是不值得的。
#6
0
Thank you all for the help. Here is what I settled on, which works.
谢谢大家的帮助。这就是我所确定的,有效的。
(from s in series select s).Average( a => a.Value )
Regards, Alan...R