linq 计数优化
我正在编写一个 linq 查询,我想知道我做得是否正确:我传递了 2 个参数,TheDate 和 UserID
Var Output = from c in in MyDC.Table1
where c.Datetime.Date == TheDate.Date
where c.UserID == TheUserID
select new MyMode()
{
Var1 = (from c1 in MyDC.Table1
where c.ID == c1.ID
select c1.ID).Count()
Var2 = ...
}
我想我应该能够对 Var1 进行计数,而无需返回到原来的MyDC,但现在这就是我所拥有的。 让我知道是否有更好的方法。
谢谢。
I'm writing a linq query and I'm wondering if I'm doing it right: I'm passing in 2 parameters, TheDate and UserID
Var Output = from c in in MyDC.Table1
where c.Datetime.Date == TheDate.Date
where c.UserID == TheUserID
select new MyMode()
{
Var1 = (from c1 in MyDC.Table1
where c.ID == c1.ID
select c1.ID).Count()
Var2 = ...
}
I'm thinking I should be able to do the count for Var1 without going back to the original MyDC but for now that's what I have.
Let me know if there's a better way to do it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这难道不等于:
您也需要对某些值进行分组吗?
Wouldn't that be the same as:
Are you needing to group on some value, too?
计数是使用基于方法的语法的覆盖。
输出:IEnumerable(5 项)
0
1
2
3
4
o 是对象,i 是索引。我通常使用方法语法,所以我不确定对其的转换。
The count is an override using method-based syntax.
outputs: IEnumerable (5 items)
0
1
2
3
4
o is the object and i is the index. I usually use method syntax so I'm not sure on the conversion to it.
如果在原始查询中按
c.DateTime.Date
和c.UserId
进行分组,则 Var1 只是组中的项目数。If you group by
c.DateTime.Date
andc.UserId
in the original query then the Var1 is just the count of items in the group.