Jarod Ferguson

Say I have Entity B which inherits Entity A. Entity B has additional fields that A does not have. I would like to query on these additional fields, but there is only a Queryable interface on the object context for A's. (B's live in the A set)

Any Ideas Can this be done

I feel like I ought to be able to use generics, or something along the lines of :

Code Snippet
context.A.Select<B>(b => b.value == myVal).First<B>();

The compiler hates me for this Smile



Re: ADO.NET (Pre-release) Querying EDM sub-types with LINQ

Erick Thompson - MSFT

Jarod,

You can use standard C# or VB type operators in a LINQ to Entities query. For example, if you wanted all the instances in the set of A that are actually B (assuming A:B), you would write the query using the is keywork.

var query = from A in context.A

where A is B

select A;

Let me know if you have any other questions,

Erick





Re: ADO.NET (Pre-release) Querying EDM sub-types with LINQ

Jarod Ferguson

Hi Erick,

Thanks, that helps. Now what I need to do is modify the query to include a condition which exists only on B types

So something like this:

Code Snippet

var query = from A in context.A

where ((B)A).SomeBProp == value

select A;

I get this error when trying that:

"LINQ to Entities supports only casts of entity data model primitive types"

In this scenario, SomeBProp comes from a different table fragment that is mapped to B only.

Jarod




Re: ADO.NET (Pre-release) Querying EDM sub-types with LINQ

Jarod Ferguson

I got it... its the OfType<> on the Queryable interface.

Code Snippet

var query = from B in context.A.OfType<B>()

where B.SomeBProp == value

select B;

Jarod





Re: ADO.NET (Pre-release) Querying EDM sub-types with LINQ

Erick Thompson - MSFT

I'm glad you were able to find that operator. Please let me know if you have any other questions on this topic.

Thanks,

Erick