Using Entity Framework to compare two distinct kinds

c# entity-framework linq linq-to-entities

Question

Assume I have a Posts table with field Owner of type Person (which has field Name).

I'm trying to make this shorthand expression work:

from p in Posts where p.Author == "SomeNameAsString" select p;

Which means I would have to compare object of type Person with a string.

I tried to override Equals and to override operators == and !=, but I'm still getting exception "DbComparisonExpression requires arguments with comparable types.".

Is there a way to make two different types comparable in Entity Framework?

1
0
1/24/2012 10:05:33 AM

Accepted Answer

EF tries to translate your query into SQL, and while doing that it doesn't seem to take into account your type conversion operators. Simply use this query instead:

from p in Posts where p.Author.Name == "SomeNameAsString" select p;
5
1/24/2012 10:06:24 AM

Popular Answer

Have you tried

 from p in Posts where p.Author.Name == "SomeNameAsString" select p; 

(if Name is a property on Author)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow