在 linq 中使用 let 运算符作为间隔
请考虑此图像:
我有一个像这样的表:
Age Active Men/Women
-------------------------------------------------
我想使用 linq toEntity 编写一个查询计算男性和女性每个年龄间隔的计数
。我可以使用 let
运算符来计算单行,但我想在一个查询中计算 All。
谢谢
please consider this image:
I have a table like this:
Age Active Men/Women
-------------------------------------------------
I want to write a query with linq to entities that calulate Count
of every age intervals for men and women. I canusing let
operator to calculate single row but I want to calculat All in one query.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这在 Linq2Sql 中是不可能的,但我想出了这个查询:
首先,我定义一个函数来返回给定年龄的 AgeGroup(
(age / 5) * 5
给出起始值年龄组的编号)。从那里开始就非常简单了。对年龄进行分组并将数字相加。编辑:
我认为在一次查询中这是不可能的。因为
group by
结束了一个查询(如select
),所以你需要先分组,然后再相加。您不能使用
IQueryable
,因为 SQL 中不知道ageGroup()
函数,因此不能在 Linq2Sql 中使用。I don’t think this is possible in Linq2Sql, but I came up with this Query:
First I define a Function to return the AgeGroup for a given age(
(age / 5) * 5
gives the starting number for a AgeGroup). From there it’s pretty straightforward. Grouping the ages and add up the numbers.Edit:
I don’t think it’s possible in one query. Because
group by
ends a query(likeselect
) and you need to group first and then add up.You can’t use
IQueryable
, because theageGroup()
Function is not know in SQL and therefore can’t be used in Linq2Sql.使用
group ... by
怎么样:http://code.msdn.microsoft.com/LINQ-to-DataSets-Grouping-c62703ea#Simple1
http://ddkonline.blogspot.com/2008 /04/linq-group-by-syntax-for-grouping-on.html
What about using
group ... by
:http://code.msdn.microsoft.com/LINQ-to-DataSets-Grouping-c62703ea#Simple1
http://ddkonline.blogspot.com/2008/04/linq-group-by-syntax-for-grouping-on.html