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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:33:55+00:00 2026-05-24T08:33:55+00:00

I am working on an app that uses Entity Framework 3.5 and an SQL

  • 0

I am working on an app that uses Entity Framework 3.5 and an SQL database. The app searches an object graph based on some search criteria passed down from a user interface. In order to handle the search functionality, I’m using a query builder with the following interface:

public interface IQueryBuilder
{
    IQueryable<SomeClass> BuildQuery(Criteria criteria);    
}

The criteria object carries several properties that the query builder uses to filter the search results. It also carries a property that dictates how the results should be sorted, called “OrderBy”. Here’s an example of the criteria object:

public class Criteria
{
    //some filter properties...

    public Expression<Func<SomeClass, object>> OrderBy { get; set; }
}

So, after the initial query has been built, the query builder sorts the query with:

var sortedQuery = query.OrderBy(criteria.OrderBy);

This works flawlessly in unit tests and integration tests (where it actually hits EF) when I order by a string property (OrderBy = x => something.LastName). BUT, when I run integration tests that order by a non-string property (OrderBy = x => something.AgeInYears), EF throw the following exception:

System.NotSupportedException: Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

In this case, something.AgeInYears is an int. EF doesn’t like the fact that I give it an int instead of an object.

SO, is there a way I can do what I’m trying to do here?

  • 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-24T08:33:57+00:00Added an answer on May 24, 2026 at 8:33 am

    I’m guessing you don’t want to (for good reason) make your criteria class like

    public class Criteria<T, U>
    {
        //some filter properties...
    
        public Expression<Func<T, U>> OrderBy { get; set; }
    }
    

    which should work…so here are a couple of alternatives:

    public class Criteria<T>
    {
        //some filter properties...
    
        public Expression OrderBy { get; set; }
    
    
        public void CreateOrdering<U>(Expression<Func<T, U>> value)
        {
            OrderBy = value;
        }
    }
    

    but since OrderBy isn’t generic typed anymore, you’d need to alter your code that applies it:

          var orderBy = Expression.Call(
                typeof(Queryable),
                "OrderBy",
                new Type[] { query.ElementType, query.ElementType },
                query.Expression,
                criteria.OrderBy);
    
          var sortedQuery = query.Provider.CreateQuery<T>(orderBy
    

    alternatively, you could handle this in the code that subsequently applies the ordering:

    instead of

    var sortedQuery = query.OrderBy(criteria.OrderBy);
    

    do something like (haven’t tested this, but should be basically like this) (UPDATE: this one has no pre-supposition that you’re using generics).

    // grab the property expression out of your ordering statement
    var expressionProperty = criteria.OrderBy.Body is UnaryExpression ? (MemberExpression)((UnaryExpression)criteria.OrderBy.Body).Operand : (MemberExpression)criteria.OrderBy.Body;
    
    // recreate the lambda as Expression<Func<T,U>> where T is the queryable element type and U 
    // is the actual type of the property, not an object
    
    var orderBy = Expression.Lambda(expressionProperty, criteria.OrderBy.Parameters.Single());
    
    // apply new lambda instead
    var sortedQuery = query.OrderBy(orderBy);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET 4 WinForms app that uses the ADO.NET Entity Framework. Some
I'm working on the app that uses CoreData. There is location entity that holds
I'm working on a C# app that uses SQL 2008 express. Sometimes I work
I am working on an mvc app that uses some silverlight to supplement a
I'm currently working on an app that uses Lion's new view-based NSOutlineView . The
I am working on a web app that uses Perl and I need to
I'm working on a stripes app that uses a bit of jQuery to make
I am working on a Java app that uses Spring IoC and JDBC Template
I've been working on a rails app that uses the restful authentication plugin. It
We have an ASP.NET 2.0 WebForms app that uses MS Ajax 1.0. It's working

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.