How to do OFTYPE ONLY in EF Code First with Linq?

When using traditional Entity Framework and querying with ESQL, you can use OFTYPE with ONLY to only return base types.

See: http://msdn.microsoft.com/en-us/library/bb399295.aspx

In Entity Framework Code First, I have inheritance set up where B is a subtype of A. Performing MyContext.Set<A>().OfType<A>() still returns elements of type B. Ideally, I would like to call MyContext.Set<A>().OfOnlyType<A>() and it would translate the same way as when using ESQL's OFTYPE ONLY.

I also found I can use the is operator in a where statement, but again, that will return both A and B entities.

How can I write a linq expression that will filter to only elements of type A?

Answers


There is no way to get only A instances directly (equivalent to ONLY) in Linq-to-entities unless you somehow exclude derived instances from the result set. For example:

var list = MyContext.Set<A>().Where(a => !(a is B)).ToList();

Need Your Help

Targeting Flash Player 11.2 - 11.4 APIs with the OpenLaszlo SWF11 runtime

flash runtime openlaszlo flash-player-11 lzx

I've read here that there is an OpenLaszlo version capable of compiling LZX into SWF11 files for Flash Player 11.1. The SDK included with the flex4.6 branch of OpenLaszlo is - as we can tell by the...

NHibernate and shared web hosting

security nhibernate web-hosting

Has anyone been able to get an NHibernate-based project up and running on a shared web host?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.