简体   繁体   中英

Comparing two different types with Entity Framework

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?

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;

Have you tried

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

(if Name is a property on Author )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM