I use linq to access a table from my DB using Entity Framework
MyDBEntities context = new MyDBEntities;
int id = 111;
var item = context.MyTable.Where(i => i.id == id).Single();
This works fine but now I create 开发者_如何学编程a method I wish to use instead of the id check:
bool AreNear(string Adress, object Adress)
I'd like to use that way
string adress = "...";
var item = context.MyTable.Where(i => AreNear(i.adress,adress) ).Single();
but I get an error at the execution saying I can't use the method in my query is there a way to make it work?
Unfortunately, there is no way to make it work.
The reason for this is that the LINQ query isn't really executed as .NET code but it is translated into SQL by the EF provider. This EF provider doesn't know how to translate AreNear
into SQL, so it fails.
精彩评论