Entityframework 6.2.0 IQueryable.Count VS IQueryable.ToList().Count

entity-framework entity-framework-core iqueryable

Question

This is a very weird problem In short

var q = (some query).Count();

Gives my a number and

var q = (some query).ToList().Count();

Gives me entirely different number...

with mentioning that (some query) has two includes (joins)

is there a sane explanation for that???

EDIT: here is my query

var q = db.membership_renewals.Include(i => i.member).Include(i => i.sport).Where(w => w.isDeleted == false).Count();

this gives me a wrong number

and this:

var q = db.membership_renewals.Include(i => i.member).Include(i => i.sport).Where(w => w.isDeleted == false).ToList().Count();

Gives me accurate number..

EDIT 2 Wher I wrote my query as linq query it worked perfectly...

var q1 = (from d in db.membership_renewals where d.isDeleted == false join m in db.members on d.mr_memberId equals m.m_id join s in db.sports on d.mr_sportId equals s.s_id select d.mr_id).Count();

I think the problem that entity framework doesn't execute the joins in the original query but forced to execute them in (ToList())...

1
1
12/9/2017 12:11:38 PM

Popular Answer

I Finally figured out what's going on... The database tables are not linked together in the database (there are no relationship or constraints defined in the database itself) so the code doesn't execute the (inner join) part. However my classes on the other hand are well written so when I perform (ToList()) it automatically ignores the unbound rows... And when I wrote the linq query defining the relation ship keys (primary and foreign) it worked alright because now the database understands my relation between tables... Thanks everyone you've been great....

0
12/9/2017 12:26:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow