Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:31:26+00:00 2026-06-15T15:31:26+00:00

I have a number of entity framework tables that I have made support an

  • 0

I have a number of entity framework tables that I have made support an interface IHistoricEntity using their parital classes. IHistoricEntity has ActiveTo Datetime? property.

// Auto generated LINQ to Entities domain service:
[EnableClientAccess()]
public partial class ProductService : LinqToEntitiesDomainService<ProductDBEntities>
{
    public IQueryable<ANALYSIS_CODES> GetANALYSIS_CODES()
    {
        return this.ObjectContext.ANALYSIS_CODES;
    }
 ...
}

// My Partial class to add interface
public partial class ANALYSIS_CODES : IHistoricEntity
{}

I am trying to refactor this working code to a method:

List<ANALYSIS_CODE> filtered = (from rec in ps.GetANALYSIS_CODES() where rec.ActiveTo == null select rec).ToList()

Like so:

private List<T> Filter<T>(IQueryable<T> queryable) where T : IHistoricEntity
{
    return (from rec in queryable where rec.ActiveTo == null select rec).ToList();
}

// called like this:
List<ANALYSIS_CODE> filtered = Filter(ps.GetANALYSIS_CODES());

This gives this exception on the ToList:

Unable to cast the type ‘ANALYSIS_CODES’ to type ‘IHistoricEntity’. LINQ to Entities only supports casting Entity Data Model primitive types.

But where have I asked it to cast to an IHistoricEntity? I have mearly said that T must support IHistoricEntity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:31:28+00:00Added an answer on June 15, 2026 at 3:31 pm

    rec.ActiveTo refers to a property defined in your interface. Therefore Linq needs to cast rec to IHistoricEntity before being able to access that property.

    Don’t be fooled by the exception being raised in .ToList(): the Linq query is only evaluated and executed when the records are needed, in this case, when the collection is to be transformed into a List<>.

    Update: I verified @hvd’s comment, and indeed, adding a where T: class clause changes the Linq expression from

    System.Collections.Generic.List`1[MyType]
        .Where(x => (Convert(x).ActiveTo == Convert(null)))
    

    to

    System.Collections.Generic.List`1[MyType]
        .Where(x => (x.ActiveTo == Convert(null)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of simple controller classes that use Doctrine's entity manager to
I'm running into a situation using the Entity Framework (EF) that has me totally
I have a number of classes (more than 40), each one has a number
I have been playing with Entity Framework and so far I like using it
I have a settings table in a database that is accessed through Entity Framework
I have a Entity Framework 5 Model First project, with the following tables: Extension
I am using the Entity Framework 4.2 and I have a fairly serious performance
Have a project that uses the entity framework (v1 with .NET 3.5). It's been
Using entity framework (with MVC, but I think that's irrelevant), I'm trying to make
I'm using ASP.NET MVC3 and Entity Framework with Database first aproach. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.