New Posts New Posts RSS Feed: Include() combined with FirstOrDefault()
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Include() combined with FirstOrDefault()

 Post Reply Post Reply
Author
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Topic: Include() combined with FirstOrDefault()
    Posted: 16-Mar-2009 at 8:47am
Ok thanks for the info.
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2009 at 4:46pm
This is actually a known bug which we'd hoped to fix in the 4.3.0 release but did not.  You can use the .Execute() or .ToList() methods as a workaround for now, but more data will be returned than wanted.  Using either of these will submit the full query to EF and the database and then invoke FirstOrDefault() on the results returned to the EM cache. 
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2009 at 11:47am
Hi,

Have you tried doing this?

em.Customer.Where(i => i.Name.BeginsWith("R")).Include("Orders").ToList().FirstOrDefault();
Back to Top
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2009 at 8:02am

Hello,

In Microsoft EF, we were able to do something like the following:

// In Microsoft EF, c.Orders.Count == 7
Customer c = em.Customer.Where(i => i.Name.BeginsWith("R")).Include("Orders").FirstOrDefault();
if (c != null)
     Console.WriteLine(c.Orders.Count);
 
And as expected, the first Customer or null would be returned, and if not null the print out of the number of orders would be accurate.  When we try the same in DevForce EF it doesn't appear to work the same.  (I need to note that we're talking straight DevForce EF here, not SL).  The Customer will come back, but the number of orders is always 0, as if the Include() is not being used.
 
// In DevForceEF, c.Orders.Count == 0
Customer c = em.Customer.Where(i => i.Name.BeginsWith("R")).Include("Orders").FirstOrDefault();
if (c != null)
     Console.WriteLine(c.Orders.Count);
 
What does seem to work is either of the following:
 
Customer c = null;
foreach (var item in em.Customer.Where(i => i.Name.BeginsWith("R")).Include("Orders"))
{
     c = item;
     break;
}
if (c != null)
     Console.WriteLine(c.Orders.Count);
 
Or:
 
Customer c = em.Customer.Where(i => i.Name.BeginsWith("R")).Include("Orders").Execute().FirstOrDefault();
if (c != null)
     Console.WriteLine(c.Orders.Count);
 
However, if we change the FirstOrDefault() to FirstOrNullEntity(), the "Execute()" solution will fail to compile.
 
Is this working as expected or are we doing something wrong?
 
Thanks,
Ken
 


Edited by ken.nelson - 13-Mar-2009 at 11:15am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down