Are these two blocks exactly the same? Does one have any advantage over the other?
using (var context = new TRANSITEntities())
{
var result = context.Table1.Where(c => c.UserCode == "123");
}
using (var context = new TRANSITEntities())
{
var result = from c in context.Table1
where c.UserCode == "123"
select c;
}
Exactly the same.
You can verify this yourself by looking at the ToString()
It should also be noted that
resultis NOT actually a result. It’s a non-executed/enumeratedIQueryable. Meaning it’s like a SQL statement that hasn’t been run yet (sort of). If you were to dothen the query would actually be executed twice.
t1andt2are the REAL results (an in memory array)…notresult. In other words,resultshould really be namedqueryAnd further, the example you have above will never work…because if you callToArrayon the result outside of theusingblock, it will fail…because you can’t run the query once the context is disposed: