Reputation: 1290
I have come across a strange scenario using the entity framework in the "Where" where by I can search for a string but not a string the is part of an array:
This works:
string line = sr.ReadLine();
string[] row = line.Split(';');
string code = row[0];
TableObject to = db.TableObject.Where(e => e.property == code).FirstOrDefault();
However If I try and simplify the code by omitting the step string code = row[0]
and doing the following:
string line = sr.ReadLine();
string[] row = line.Split(';');
TableObject to = db.TableObject.Where(e => e.property == (string) row[0]).FirstOrDefault();
I get the following exception:
System.NotSupportedException: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.
I find it strage that the excepption is complaining about an Array seeing as I have specificaly cast the row[0]
array as a (string)
.
.ToString()
does not solve the problem either.
I know this is no big deal but I am curious as to why this is.
Upvotes: 2
Views: 87
Reputation: 51654
Entity Framework tries to translate the complete expression e => e.property == (string) row[0]
into a SQL WHERE
statement. It just doesn't know what SQL expression to translate (string) row[0]
into.
Every LINQ provider can only handle a set of specific instructions. The EF LINQ provider doesn't support evaluating an array's index. It can only use primitives. That's why you have to extract the string from the array outside the lambda.
Upvotes: 2