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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:02:01+00:00 2026-05-26T09:02:01+00:00

I have an MVC3 project using the Entity Framework model in which I’ve marked

  • 0

I have an MVC3 project using the Entity Framework model in which I’ve marked up a class like this:

public partial class Product
{
    public bool IsShipped
    {
        get { /* do stuff */ }
    }
}

and which I want to use in a LINQ expression:

db.Products.Where(x => x.IsShipped).Select(...);

however, I get the following error:

System.NotSupportedException was unhandled by user code Message=The
specified type member ‘IsShipped’ is not supported in LINQ to Entities.
Only initializers, entity members, and entity navigation properties
are supported. Source=System.Data.Entity

I’ve googled but not found anything definitive about this usage to I tried:

public partial class Product
{
    public bool IsShipped()
    {
        /* do stuff */
    }
}

db.Products.Where(x => x.IsShipped()).Select(...);

but then I get:

System.NotSupportedException was unhandled by user code Message=LINQ
to Entities does not recognize the method ‘Boolean IsShipped()’ method,
and this method cannot be translated into a store expression.
Source=System.Data.Entity

there’s functionality there that I don’t want to build into the LINQ query itself… what’s a good way to handle this?

* update *

Darin makes the valid point that whatever is done in the implementation of IsShipped would need to be converted to a SQL query and the compiler probably doesn’t know how to do it, thus retrieving all objects into memory seems the only choice (unless a direct query to the database is made). I tried it like this:

IEnumerable<Product> xp = db.Quizes
    .ToList()
    .Where(x => !x.IsShipped)
    .Select(x => x.Component.Product);

but it generates this error:

A relationship multiplicity constraint violation occurred: An
EntityReference can have no more than one related object, but the
query returned more than one related object. This is a non-recoverable
error.

though curiously this works:

IEnumerable<Product> xp = db.Quizes
    .ToList()
    .Where(x => x.Skill.Id == 3)
    .Select(x => x.Component.Product);

why would that be?

* update II *

sorry, that last statement doesn’t work either…

* update III *

I’m closing this question in favour of pursuing a solution as suggested here to flatten my logic into a query – the discussion will move to this new post. The second alternative, to retrieve the entire original query into memory, is likely unacceptable, but the third, of implementing the logic as a direct query to the database, remain to be explored.

Thanks everyone for the valuable input.

  • 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-26T09:02:01+00:00Added an answer on May 26, 2026 at 9:02 am

    The only way to make this “DRY” (avoid repeating the logic inside of IsShipped in the Where clause again) and to avoid loading all data into memory before you apply the filter is to extract the content of IsShipped into an expression. You can then use this expression as parameter to Where and in IsShipped as well. Example:

    public partial class Product
    {
        public int ProductId { get; set; }           // <- mapped to DB
        public DateTime? ShippingDate { get; set; }  // <- mapped to DB
        public int ShippedQuantity { get; set; }     // <- mapped to DB
    
        // Static expression which must be understood
        // by LINQ to Entities, i.e. translatable into SQL
        public static Expression<Func<Product, bool>> IsShippedExpression
        {
            get { return p => p.ShippingDate.HasValue && p.ShippedQuantity > 0; }
        }
    
        public bool IsShipped // <- not mapped to DB because readonly
        {
            // Compile expression into delegate Func<Product, bool>
            // and execute delegate
            get { return Product.IsShippedExpression.Compile()(this); }
        }
    }
    

    The you can perform the query like so:

    var result = db.Products.Where(Product.IsShippedExpression).Select(...).ToList();
    

    Here you would have only one place to put the logic in (IsShippedExpression) and then use it for database queries and in your IsShipped property as well.

    Would I do this? In most cases probably no, because compiling the expression is slow. Unless the logic is very complex, likely a subject to change and I am in a situation where the performance of using IsShipped doesn’t matter, I would repeat the logic. It’s always possible to extract often used filters into an extension method:

    public static class MyQueryExtensions
    {
        public static IQueryable<Product> WhereIsShipped(
            this IQueryable<Product> query)
        {
            return query.Where(p => p.ShippingDate.HasValue && p.ShippedQuantity >0);
        }
    }
    

    And then use it this way:

    var result = db.Products.WhereIsShipped().Select(...).ToList();
    

    You would have two places though the maintain the logic: the IsShipped property and the extension method, but then you can reuse it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a project using MVC3 and Code first Entity Framework 4.0 as
I have upgrade my MVC2 project to MVC3 using this tool: http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx Which is
I'm working with MVC3, and using Entity Framework 4.0 Entities as my model. So
I have an MVC 3 project that I am working on using Entity Framework
I have an ASP.NET MVC 3 (using Entity Framework 4.2) application that uses transactions
I have a very simple .NET MVC3 project set up using jquery mobile for
I am currently programming a ASP.NET MVC3 application, using Entity Framework 4. There are
I have a new MVC3 project with one Controller called PublicController.cs which contains 4
First some context: I have an MVC3 .net project which, for the sake of
I have a web project using asp.net mvc3. Now clients ask for a security

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.