LINQ to entities exception
I'm trying to pull out an object from a DbSet with a Linq query.
I use the syntax:
Nation nation = nationDB.Nations.Where(c => c.ID == testNation.ID).First();
I get the following exc开发者_如何学JAVAeption:
LINQ to Entities does not recognize the method 'Nation get_Item(Int32)' method, and this method cannot be translated into a store expression.
The class Nation
has a couple of string
fields, one int
field (the ID) and a couple of other objects as fields.
What can the problem be?
You need to pull out the integer first because there is no appropriate translation for retrieving the integer from your object Nation
within the Linq to Entities scope:
int testId = testNation.ID;
Nation nation = nationDB.Nations.Where(c => c.ID == testId).SingleOrDefault();
If ID
is of type int
then shouldn't the line be:
Nation nation = nationDB.Nations.Where(c => c.ID == 1).SingleOrDefault();
Removed the quotes to do an int
to int
compare.
so if you say " one int field (the ID) " and .Where(c => c.ID == "1")
i think you need change to
.Where(c => c.ID == 1)
精彩评论