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 925413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:30:59+00:00 2026-05-15T19:30:59+00:00

Following on from the excellent answer to my previous question: Linq Entity Framework generic

  • 0

Following on from the excellent answer to my previous question:

Linq Entity Framework generic filter method

I am now trying to understand how I can apply something like recursion to my solution.

To recap, instead of multiple similar declarations of this method:

protected IQueryable<Database.Product> GetActiveProducts( ObjectSet<Database.Product> products ) {

    var allowedStates = new string[] { "Active" , "Pending" };

    return (
        from product in products
        where allowedStates.Contains( product.State )
            && product.Hidden == "No"
        select product
    );

}

I now have a single implementation that accepts an interface type (IHideable) to operate on:

protected IQueryable<TEntity> GetActiveEntities<TEntity>( ObjectSet<TEntity> entities ) where TEntity : class , Database.IHideable {

    var allowedStates = new string[] { "Active" , "Pending" };

    return (
        from entity in entities
        where allowedStates.Contains( entity.State )
            && entity.Hidden == "No"
        select entity
    );

}

This works well and the solution is clean and understandable ( big thanks to https://stackoverflow.com/users/263693/stephen-cleary ).

What I am trying to do now is to apply a similar (or the same?) method to any EntityCollections associated to an EntityObject that also happen to implement IHideable.

I currently make use of GetActiveEntities() like this:

var products = GetActiveEntities( Entities.Products );

return (

    from product in products

    let latestOrder = product.Orders.FirstOrDefault( 
        candidateOrder => (
            candidateOrder.Date == product.Orders.Max( maxOrder => maxOrder.Date )
        )       
    )

    select new Product() {

        Id = product.Id ,
        Name = product.Name,
        LatestOrder = new Order() {

            Id = latestOrder.Id ,
            Amount = latestOrder.Amount,
            Date = latestOrder.Date

        }

    }

);

In this example I would like to have the Orders EntityCollection also filter by GetActiveEntities(), so that the latest order returned can never be a “hidden” one.

Is it possible to have all EntityCollections implementing IHideable to be filtered – maybe by applying some reflection/recursion inside GetActiveEntities() and calling itself? I say recursion because the best solution would go multiple levels deep walking through the entity graph.

This stuff is stretching my brain!

UPDATE #1 (moved my comments up to here)

Thanks Steve.

Making the method accept IQuerable as suggested gives this error:

'System.Data.Objects.DataClasses.EntityCollection<Database.Order>' does not contain a definition for 'GetActiveEntities' and no extension method 'GetActiveEntities' accepting a first argument of type 'System.Data.Objects.DataClasses.EntityCollection<Database.Order>' could be found (are you missing a using directive or an assembly reference?)

I presume this is because EntityCollection does not implement IQueryable.

I was able to get further by creating a second extension method that explicitly accepted EntityCollection and returned IEnumerable. That compiled but at runtime gave this error:

LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[Database.Order] GetActiveEntities[Order](System.Data.Objects.DataClasses.EntityCollection`1[Database.Order])' method, and this method cannot be translated into a store expression.

I also tried calling AsQueryable() on the EntityCollection and returning IQueryable but the same error came back.

  • 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-05-15T19:31:00+00:00Added an answer on May 15, 2026 at 7:31 pm

    Edited to use ObjectSet instead of EntityCollection

    I recommend using my solution, but as an extension method, which can be applied to any query:

    public static IQueryable<TEntity> WhereActive<TEntity>(
        this IQueryable<TEntity> entities)
        where TEntity : class , Database.IHideable
    {
        var allowedStates = new string[] { "Active", "Pending" };
    
        return (
            from entity in entities
            where allowedStates.Contains(entity.State)
                && entity.Hidden == "No"
            select entity
        );
    }  
    

    This can then be used as such:

    var products = Entities.Products.WhereActive();
    
    return (
    
        from product in products
    
        let latestOrder = (
            from order in Entities.Orders.WhereActive()
            where order.ProductId == product.Id
            orderby candidateOrder.Date descending
            select order).FirstOrDefault()
    
        select new Product()
        {
            Id = product.Id,
            Name = product.Name,
            LatestOrder = new Order()
            {
                Id = latestOrder.Id,
                Amount = latestOrder.Amount,
                Date = latestOrder.Date
            }
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question , I now want to know how to stop
Following on from this question I now have code that can attach to a
Following on from my recent question on Large, Complex Objects as a Web Service
Following on from this question what would be the best way to write a
Following on from the question asked by Mykroft Best way to handle input from
Following up from this question: How can I unlock a file that is locked
Following on from my question on using frozen Capistrano a couple of days back
Following on from this question, what would be the best way to represent a
Following on from my recent question regarding parsing XML files in Java I have
I've been searching around trying to find an answer to this question, and I

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.