Searching For A Record After A LINQ query
Posted
by
Justin
on Stack Overflow
See other posts from Stack Overflow
or by Justin
Published on 2013-11-11T15:07:20Z
Indexed on
2013/11/11
15:53 UTC
Read the original article
Hit count: 212
I'm confused to why this is happening. I'm new to LINQ so I'm clearly missing something here, that is probably pretty easy. I've looked up help on the topic, but I don't really know what to ask so I haven't found any answers that really address my question.
This doesn't work
It throws an EntityCommandExecutionException
when the FirstOrDefault
method is executed.
var query = from band in context.BandsEntitySet
where band.ID == 12345
select band;
string venueName = "Willis Park";
foreach (var item in query)
{
var venue = context.VenueEntitySet.FirstOrDefault(r => r.Venue.Equals(venueName));
}
This works
var query = from band in context.BandsEntitySet
where band.ID == 12345
select band;
var bandList = query.toList();
string venueName = "Willis Park";
foreach (var item in bandList)
{
var venue = context.VenueEntitySet.FirstOrDefault(r => r.Venue.Equals(venueName));
}
My question is simple: Why is the exception being thrown? And why does creating a list from the query allow me to use the FirstOrDefault
method?
Exception Message: A first chance exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll
I guess I am wrong in my assumption that query is a list? Then what is it exactly? I'm confused because this doesn't throw an exception:
foreach (var item in query)
{
var area = item.VenueArea;
}
I'd appreciate any help on this issue.
thanks, Justin
© Stack Overflow or respective owner