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

  • Home
  • SEARCH
  • 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 8654873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:53:36+00:00 2026-06-12T14:53:36+00:00

I want to do something that feels like it should be really simple, but

  • 0

I want to do something that feels like it should be really simple, but has turned out to be a real headache. I’m Selecting from an IQueryable and instead of creating a new object, I want to “tweak” the one that I’m projecting. Here’s what I want to be able to do (context is a DbContext, and I’m using a code-first approach):

var eventsToReturn =
    ((IQueryable<Event>)
        context.Events
        .OrderByDescending(evnt => evnt.TakesPlaceOn)
        .Skip(startAt)
        .Take(numberToReturn)
    )
    .Select(evnt =>
        {
            evnt.UnmappedUsersCount = evnt.Users.Count();
            return evnt;
        }
    )
    .ToList();

This is nice because Entity Framework doesn’t have to actually get all the User entities associated with each event, and can use an aggregate COUNT function to simply get the number of users. However, it doesn’t work, because the IQueryable version of Select wants a lambda statement with no method body that can be converted into an expression tree. So, while the above doesn’t work, this does:

.Select(evnt => evnt)

… and so does this:

.Select(evnt => evnt.Users.Count())

I have solved the problem, but my way seems rather long-winded. Here’s my approach:

var eventsToReturnData =
    ((IQueryable<Event>)
        context.Events
        .OrderByDescending(evnt => evnt.TakesPlaceOn)
        .Skip(startAt)
        .Take(numberToReturn)
    )
    .Select(evnt =>
        new {
            Event = evnt,
            UnmappedUsersCount = evnt.Users.Count()
        }
    )
    .ToList();

var eventsToReturn = eventsToReturnData
    .Select(evntData => {
        evntData.Event.UnmappedUsersCount = evntData.UnmappedUsersCount;
        return evntData.Event;
    })
    .ToList();

So I’m projecting the main Event object into an anonymous object, as well as the one tiny int that I want to add to it, and then doing a whole new Select on eventsToReturnData, which works this time because eventsToReturnData is an IEnumerable (so the lambda can have a method body), just to tweak the Event object by setting the UnmappedUsersCount integer that I got from the first query.

Is there a simpler or more elegant way to do this? I want to avoid EF having to retreive all the users for each event, so it’s no good just using Event.Users.Count() on what is returned by the IQueryable‘s Select method.

  • 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-12T14:53:38+00:00Added an answer on June 12, 2026 at 2:53 pm

    You could leverage the Expression class to build a tree at runtime but that is so much work and code that it would be justified only if you had to do such a thing a lot of times.

    If this pattern occurs only once or rarely in your code this is the best you can do.

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

Sidebar

Related Questions

Q1. I want to grep something like that: grep -Ir --exclude-dir=some*dirs my-text ~/somewhere but
I want something that will receive me the processes details, like I receive with
I want to make something that makes much sense, but I think its not
this is something that i don't want to program, but i was looking for
I want to prompt the user that something special has happened, so I want
What a want to do is something like that: declare @id int set @id
I want to have a route that looks something like: www.abc.com/companyName/Controller/Action/Id However, all the
I feel like the answer to this question is really simple, but I really
I feel silly for asking something that seems like it should be so easy
I feel like this is something that Google should be able to solve for

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.