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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:49:20+00:00 2026-05-17T22:49:20+00:00

I have a View Model that has some serious nesting. I need to populate

  • 0

I have a View Model that has some serious nesting. I need to populate it from Entity Framework 4. I tried creating one big linq statement to populate it, but it says it doesn’t recognize the .ToList() methods. It compiles fine. Runtime error is

LINQ to Entities does not recognize the method 
'System.Collections.Generic.List`1[ProductDepartment] ToList[ProductDepartment]
(System.Collections.Generic.IEnumerable`1[ProductDepartment])' method, 
and this method cannot be translated into a store expression.

What is a more efficient way to populate something like this without doing several thousand database calls?

List<Product> Products {
    int ID
    string Name
    ...
    List<Department> Departments {
        int ID
        string Name
    }
    List<Image> Images {
        int ID
        string Name
    }
    List<Price> Prices {
        int ID
        string Name
        List<Version> Versions {
            int ID
            string Name
            List<Pages> Pages {
                int ID
                string Name
}   }   }   }

The horrible Linq code looks something like this

var myProducts = (from myProduct in DC.MyProducts
                  where p => p.productGroup == 1
                  select new Product {
                      ID = myProduct.ID,
                      Name = myProduct.Name,
                      Departments = (from myDept in DC.MyDepartments
                                     where q => q.fkey = myProduct.pkey
                                     select new Department {
                                         ID = myDept.ID,
                                         Name = myDept.Name
                                     }).ToList(),
                      ...
                      //Same field assignment with each nesting
                  }).ToList();

Update:

The fix was to remove all the .ToLists(), which worked better anyway.

Now I have to do filtering and sorting on the end product.

  • 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-17T22:49:20+00:00Added an answer on May 17, 2026 at 10:49 pm

    Well for starters, that is one crazy model, but i’m assuming you already know this.

    Do you really need all that info at once?

    I’ll play devil’s advocate here and assume you do, in which case you have a couple of logical choices:

    1) As @xandy mentioned – use .Include to eager load your associations in the one call. This is assuming you have setup navigational properties for your entites in your EDMX.

    2) Use a View. Put all that crazy joining logic inside the database, making your EF work a very simple select from the view. The downside of this is your queries to the view basically become read only, as i don’t believe you can perform updates to an entity which is mapped to a view.

    So it’s your choice – if this is a readonly collection for displaying data, use a View, otherwise eager-load your associations in the one hit.

    Also, be careful when writing your LINQ queries – i see you have multiple .ToList statements, which will cause the query to be executed.

    Build up your query, then perform the .ToList once at the end.

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

Sidebar

Related Questions

I have a Entity Framework model class that has a view to create an
I have a View Model class in a WPF application that has some very
I have a view with a model that has several fields. When I render
I have a Backbone View that has a Collection as its Model. If the
I have a django auth user proxy model that has some extra permissions attached
I have a model that contains a dictionary property. (this has been distilled from
I have ViewModel that has inside a Model and some extra properties. There are
Let's say I have an Entity in my Entity Framework Model that is named
I have a view which validates data from a form which just has some
I have a view model that looks like this public class ViewModelRound2 { public

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.