.Count() in Linq expressions

8 views
Skip to first unread message

Steve

unread,
Aug 21, 2009, 12:02:29 PM8/21/09
to nhibernate-development
Quick question. For a Linq expression of the following form:

(from c in Customers select c).Count()

can anyone think of any scenario where the projection part of the
expression (i.e., the select bit) is useful? (I'm assuming that they
don't have anything in there that causes side effects).

My reasoning right now is that the contents of the select are
irrelevant and can just be swapped with a "count(*)".

Anyone want to prove me wrong?

Cheers,

Steve

Kim Johansson

unread,
Aug 21, 2009, 1:18:53 PM8/21/09
to nhibernate-...@googlegroups.com
LINQ syntax is stupid and long.

int count = (from c in Customers select c).Count();

can be replaced with

int count = Customers.Count();

Fabio Maulo

unread,
Aug 21, 2009, 1:23:54 PM8/21/09
to nhibernate-...@googlegroups.com
You are right.
I have another question but perhaps is for another thread.

2009/8/21 Steve <srst...@gmail.com>



--
Fabio Maulo

Bruno Calzetta L.

unread,
Aug 21, 2009, 1:26:14 PM8/21/09
to nhibernate-...@googlegroups.com
Hi Steve,
I think could be some problems using count() and distinct() on the same query...
 
Regards
Bruno

2009/8/21 Steve <srst...@gmail.com>

Chad Lee

unread,
Aug 21, 2009, 1:29:43 PM8/21/09
to nhibernate-...@googlegroups.com
I can't think of any off-hand.  Even if there is something like a subquery in the select, that shouldn't affect doing a Count() on the root query.

Ayende Rahien

unread,
Aug 21, 2009, 2:36:27 PM8/21/09
to nhibernate-...@googlegroups.com
(from c in Customers select c.Name).Count() <-- in DB, will return only non null results.

Steve Strong

unread,
Aug 21, 2009, 5:03:20 PM8/21/09
to nhibernate-...@googlegroups.com
Yup, good point. That's why I asked, it's so easy to miss those edge cases  (although that is quite a large edge to have missed!)

Cheers,

Steve

Stefan Wenig

unread,
Aug 24, 2009, 6:33:31 AM8/24/09
to nhibernate-development
On Aug 21, 8:36 pm, Ayende Rahien <aye...@ayende.com> wrote:
> (from c in Customers select c.Name).Count() <-- in DB, will return only non
> null results.

Yes, but what's the conclusion?

HQL, like SQL, has count(*), count(all...) and count(distinct). The
LINQ
Count() method has the same semantics as count(*), so this should
always be used. Mapping (... select c.Name).Count() to
count ([all] c.Name) is wrong IMO, because it does not preserve the
semantics of the LINQ statement.

If that's what you want in LINQ, you need to write

(from c in Customers where c.Name != null select c).Count() // or
"select c.Name", same difference
or
(from c in Customers select c.Name).Count(n => n != null)

@Steve: in re-linq, the predicate of Count (predicate) is already
translated to a where clause. The transformation from c.Name to
count(*) should be rather simple. If you find more difficult
structural
translations, we could talk about having them prepared in re-linq.

Cheers,
Stefan
Reply all
Reply to author
Forward
0 new messages