Francesco
Francesco

Reputation: 964

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 exception:

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?

Upvotes: 0

Views: 262

Answers (3)

BrokenGlass
BrokenGlass

Reputation: 160892

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();

Upvotes: 2

Kelsey
Kelsey

Reputation: 47726

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.

Upvotes: 2

Sanja Melnichuk
Sanja Melnichuk

Reputation: 3505

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)

Upvotes: 0

Related Questions