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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:56:36+00:00 2026-05-20T00:56:36+00:00

I have a unit of work and a repository using EF 4 and POCOs.

  • 0

I have a unit of work and a repository using EF 4 and POCOs. Since EF requires an ordered set before it can Skip() and Take(), I added the following unit test (without mocks) just to pull out a record to see if it worked.

var myList = UOW.EntityRepo.Get( orderbyLambda: p => p.ID, page: 1, pageSize: 1);

This results in an expression of orderbyLambda = {p => Convert(p.ID)} and an error during enumeration. The ID is a tinyint (Int16 / short)

So why does this fail to order by the ID? More about the error

Unable to cast the type 'System.Int16' to type 'System.Object'.

I define the orderbyLambda as Expression<Func<E, object>> orderbyLambda

EDIT:

The real killer is if I do this:

 orderbyLambda:  p => new { p.ID }

It works… Why?

  • 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-20T00:56:37+00:00Added an answer on May 20, 2026 at 12:56 am

    It is spotting “order by {object}” and panicking; it knows how to order by string, int, short, DateTime, etc – but object is a bit too vague.

    You’re going to need the actual lambda to be typed correctly; the simplest approach would be to make Get generic, i.e.

    .... Get<TIdentity>(
             Expression<Func<E, TIdentity>> orderbyLambda, int page, int pageSize)
    

    and then:

    orderbyLambda: p => p.ID
    

    should (without you changing the code at the caller) automatically make that a Get<short>(...) in this case via generic type inference. The other option is to leave it as <E,object>, but re-write the expression tree at the receiver. More work.

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

Sidebar

Related Questions

I am using EF4.3 with a unit-of-work + repository pattern. I have a method
Cheers!I have some doubts about using Unit of Work with Repository. Specially a role
I have been using http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application as guidance to help me create a repository.I have
I have created a generic unit of work/repository pattern and it works in most
Currently in my application and using the unit of work pattern and generic repository,
I have an MVC3 site with EF4.1 and repository/unit of work pattern and creating
First, I am using EF 4.1 and utilizing the Repository and Unit Of Work
I have a bunch of repository classes which I want to unit-test using Visual
i am using Entity Framework and repository with unit of work design pattern. Sometimes
I have this code that works in a unit test but doesn't work when

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.