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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:44:04+00:00 2026-05-21T06:44:04+00:00

I am using ASP.NET MVC 3 with Entity Framework 4 using POCOs and want

  • 0

I am using ASP.NET MVC 3 with Entity Framework 4 using POCOs and want to query a set and select some properties to put into my viewModel. I will sketch a simplified version of my situation:

Situation:

I have an entity BananaTree containing a collection of Banana

public class Banana
{
    public int Id { get; set; }
    public int Size { get; set; }
    public TimeSpan Age { get; set }
    public string Description { get; set; }
}

public class BananaTree
{
    public int Id { get; set; }
    public ICollection<Banana> Bananas { get; set; }
}

I also have a view model BananaListItemViewModel used in the view showing a list of bananas for a certain banana tree. This view is managed by the BananaTreeController

public class BananaListItemViewModel
{
    public int Id { get; set; }
    public TimeSpan Age { get; set }
}

I have a Details action on the controller like so:

public ActionResult Details(int bananaTreeId)
{
    var viewModel = from bananaTree in bananaTreeRepository.BananaTrees
                    where bananaTree.Id == bananaTreeId
                    from banana in bananaTree.Bananas
                    select new BananaListItemViewModel
                    {
                        Id = banana.Id,
                        Age = banana.Age
                    };

    return View(viewModel);
}

What I want to change

This works fine and now I only select the items from the database that I need for my view model. However, I want to take out some more logic from my controller and am trying to do this as much as possible.

I would like to have a function in my repository like so:

IQueryable<Banana> GetBananas(int bananaTreeId)
{
    return (from bananaTree in BananaTrees
            where bananaTree.Id == bananaTreeId
            select bananaTree.Bananas).Single().AsQueryable();
}

and use it like so:

public ActionResult Details(int bananaTreeId)
{
    var viewModel = from banana in bananaTreeRepository.GetBananas(bananaTreeId)
                    select new BananaListItemViewModel
                    {
                        Id = banana.Id,
                        Age = banana.Age
                    };

    return View(viewModel);
}

Question

My question is, in this case, will the two queries be combined and go to the database in one go like in my first example or will this first get all the bananas from the tree completely out of the database and perform the second query on that list? I would prefer the first case. If not, could I rewrite the GetBananas query to get that behaviour (for example like the query below)?

IQueryable<Banana> GetBananas(int bananaTreeId)
{
    return from bananaTree in BananaTrees
           where bananaTree.Id == bananaTreeId
           from banana in bananaTree.Bananas
           select banana;
}

Thanks very much in advance.

  • 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-21T06:44:04+00:00Added an answer on May 21, 2026 at 6:44 am

    In your specific case, it will be only one query, if the call to Single() doesn’t lead to the query to be executed. Unfortunately, I couldn’t find any info on whether it does or does not. The call to AsQueryable does not trigger the execution as long, as the Bananas property really is an IQueryable.
    According to http://msdn.microsoft.com/en-us/library/bb156472.aspx, the call to Single doesn’t execute your query.
    Conclusion:
    You code should result in only one query.

    In general:
    You can pass an IQueryable from one method to another without it being implicitly executed.
    The following code will result in only one SQL statement executed at the end, when the call to ToList happens:

    IQueryable<Banana> GetBananasByWeight(int weight)
    {
        return from banana in Bananas where banana.Weight = weight;
    }
    
    IQueryable<Banana> FilterByQuality(IQueryable<Banana> bananaQuery, int quality)
    {
        return bananaQuery.Where(b => b.Quality == quality);
    }
    
    public List<Banana> GetBananas(int weight, int quality)
    {
        var query = GetBananasByWeight(weight);
        var filteredBananas = FilterByQuality(query, quality);
        return filteredBananas.ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using ASP.NET MVC and Entity Framework, I encounter some attach/detach errors when I need
I've been doing some research on using ASP.NET MVC and Entity Framework together for
Using ASP.NET MVC, Entity framework, jQuery. I want to give users the ability to
I'm in ASP.NET MVC and am (mostly) using Entity Framework. I want to call
When using ASP.NET MVC plus Entity Framework, and trying to implement a generic repository
I am using the asp.net mvc with the Entity Framework. I have a list
i've started working with ASP.NET MVC and using Entity Framework, concretely with SQLite db,
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1 and
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1, however,
I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My

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.