开发者

LINQ to entities exception

开发者 https://www.devze.com 2023-02-18 23:47 出处:网络
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'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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号