Monday, 15 August 2011

c# - Entity Framework - Get row count of a (group by Subquery) -



c# - Entity Framework - Get row count of a (group by Subquery) -

i have simple look each order's amount:

public iqueryable<orders> getaccountsummery() { homecoming context.orders.groupby(a => new { orderno = a.orderno }) .select(b => new { orderno = b.key.orderno, amount = b.sum(r => r.amount) }); }

i needed total number of records returned previous expression:

sql

select count(1) ( select orderno,sum(amount) amount orders grouping orderno )tbl -- 125,000 row count here

ef

public int getorderscount() { homecoming getaccountsummery().count(); // guy here gives 198,000 rows counts rows orders table // next line gives right row count: homecoming getaccountsummery().asenumerable().count(); // 125,000 row }

the problem getaccountsummery().asenumerable().count() runs query first @ server side calculates right row count @ client side (consider table size here)

is there way right count without executing select statement ?

edit

if not possible groupby subquery, why where subs?

the way have structured? no. you'll execute .select()statement because .count() employs immediate execution (see here list). however, .count() has override takes in func, should able utilize grab count without having perform select first, so:

context.orders.groupby(a => new { orderno = a.orderno }) .count(a => a.key.orderno != string.empty);

edit: whoops, forgot bool. edited accordingly.

edit 2: per comments, don't think possible, since you'll need have select in there @ given time, , .count() phone call it. sorry.

c# sql-server entity-framework linq-to-entities

No comments:

Post a Comment