Linq返回count时选择什么?
我养成了这样做的愚蠢习惯:
return
(from c in db.tblUserAlerts
where c.userID == UserID && !c.hasRead
select new { c.ID }).Count();
按照整个 c
记录的要求返回 c.ID
是否更好(节省内存)?例子:
return
(from c in db.tblUserAlerts
where c.userID == UserID && !c.hasRead
select c).Count();
I've gotten in the silly habit of doing this:
return
(from c in db.tblUserAlerts
where c.userID == UserID && !c.hasRead
select new { c.ID }).Count();
Is it better (saves memory) to return c.ID
as supposed to the entire c
record? Example:
return
(from c in db.tblUserAlerts
where c.userID == UserID && !c.hasRead
select c).Count();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题更多的是数据必须经过多少转换。对于您想要做的事情,我认为最有效的是:
因为您实际上只通过一种方法运行数据,并且它不会复制或转换您的数据,而只是计算与谓词匹配的内容。
编辑:我再次查看了您的标签,发现您有 linq-to-sql。如果您使用 LINQ to SQL 等查询提供程序,则会转换查询。我猜想 linq-to-sql 查询翻译器会翻译成这样:
在这种情况下,不应该有任何速度差异,除了 LINQ-to-SQL 可能需要在翻译查询时做更多的工作,因为有表达式树中的更多内容。这种速度差异几乎可以忽略不计。我上面的原始答案更适用于 LINQ-to-Objects。对于 LINQ-to-SQL,除非您对任何速度下降都非常敏感(即您在紧密循环中运行数千次),否则它们应该大致相同。
I think the issue is more how much transformation the data has to go through. For what you are trying to do, I think the most efficient would be:
because you are really only running your data through 1 method and it isn't copying or transforming your data, just counting the things that match the predicate.
EDIT: I looked again at your tags, and see that you have linq-to-sql. If you are using a query provider like LINQ to SQL, the query is translated. I would guess the linq-to-sql query translator would translate to something like:
in this case there shouldn't be any speed difference whatsoever, except that LINQ-to-SQL might have to do more work in translating your query because there is more in the expression tree. That speed difference would be mostly negligible. My original answer above is more applicable to LINQ-to-Objects. With LINQ-to-SQL unless you are hyper sensitive to any speed decrease (ie you are running this thousands of times in a tight loop) they should be roughly equivalent.
编写此查询的最明显的方法是按照建议的。
在这种情况下,SQL 可以执行
COUNT (1) WHERE...
您的选择不太可能给您带来任何损失或收益 - 它会被转换为 SQL,并且
的执行差异COUNT(*)
和COUNT(id)
将会很小(如果存在),COUNT(id)
的执行差异也会很小和COUNT(1)
。但为什么要编写不执行任何操作的代码呢?无需选择任何内容。你问的似乎很奇怪
但随后发表评论
您想编写高效、干净的代码来“节省内存”,还是想根据自己的喜好编写,没有明显的原因?更少的代码就是更干净的代码 - 这并不是说
.Count(Exp<>)
是一个晦涩难懂、很少使用的重载。The most obvious way to write this query is as suggested.
In this case SQL could execute a
COUNT (1) WHERE...
It's very unlikely that your select is going to cost or gain you anything - it's translated into SQL, and the execution difference of
COUNT(*)
andCOUNT(id)
is going to be tiny (if it exists at all), as will the execution difference ofCOUNT(id)
andCOUNT(1)
. But why write code that doesn't do anything? There's no need to select anything.It seems strange that you ask
but then comment
Do you want to write efficient, clean code that 'saves memory', or do you want to write according to your preferences for no obvious reason? Less code is cleaner code - it's not as if
.Count(Exp<>)
is an obscure, rarely used overload.