Muflix
Muflix

Reputation: 6798

LINQ to Entities does not recognize the method - Include + Where

I have this working Code

var h = db.MyTable.Include("Children").Include("Parent").ToList();

but when I add where condition

var h = db.MyTable.Include("Children").Include("Parent").Where(x => x.Country==Session["country"].ToString()).ToList();

it throws me an error

LINQ to Entities does not recognize the method 'System.Object get_item (System.String)' method , and this method cannot be translated into a store expression.

How can i rewrite it ? Im beginner :-) Thank you

Upvotes: 3

Views: 3326

Answers (2)

Frank
Frank

Reputation: 4481

This is caused by your Where-Expression. You should only use variables in there or call methods that can be translated to SQL.

First, save your Session value in a variable:

var country = Session["country"].ToString();

Then use country in your Where-Expression.

MSDN provides a list of methods you can use inside LINQ to SQL expressions and how they are mapped to SQL functions.

Upvotes: 3

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

try this:

var t = Session["country"].ToString();
var h = db.MyTable.Include("Children").Include("Parent").Where(x => x.Country==t).ToList();

only activities can be parsed into expression-tree that is supported by your provider

Upvotes: 1

Related Questions