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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:04:30+00:00 2026-06-09T16:04:30+00:00

I have an ASP.NET MVC application which uses Entity Framework to get data. I

  • 0

I have an ASP.NET MVC application which uses Entity Framework to get data.

I need to transform Entites to Models before passing them to View. Projections can be very complex, but to keep it simple:

public static IQueryable<UserModel> ToModel(this IQueryable<User> users)
{
    return from user in users
           select new UserModel
           {
               Name = user.Name,
               Email = user.Email,
           };
}

This can be used in a controller like this:

return View(Repository.Users.ToModel().ToList());

Very good. But what if I want to use this projection inside another one? Example:

public static IQueryable<BlogPostModel> ToModel(this IQueryable<BlogPost> blogs)
{
    return from blogs in blogs
           select new BlogPostModel
           {
               Title = blog.Title,
               Authors = blog.Authors.AsQueryable().ToModel(), // (entities are POCOs)
               // This does not work, because EF does not understand method ToModel().
           };
}

(let’s suppose blog can have more then one author and it is of type User).

Can I somehow separate the projections and reuse them inside another ones?

  • 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-09T16:04:32+00:00Added an answer on June 9, 2026 at 4:04 pm

    Here’s something that actually works (in a simple test application) to only select the requested fields:

    namespace Entities
    {
        public class BlogPost
        {
            public virtual int Id { get; set; }
            public virtual string Title { get; set; }
            public virtual DateTime Created { get; set; }
            public virtual ICollection<User> Authors { get; set; }
        }
    
        public class User
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual string Email { get; set; }
            public virtual byte[] Password { get; set; }
            public virtual ICollection<BlogPost> BlogPosts { get; set; }
        }
    }
    
    namespace Models
    {
        public class BlogPostModel
        {
            public string Title { get; set; }
            public IEnumerable<UserModel> Authors { get; set; }
        }
    
        public class UserModel
        {
            public string Name { get; set; }
            public string Email { get; set; }
        }
    
        public static class BlogPostModelExtensions
        {
            public static readonly Expression<Func<BlogPost, BlogPostModel>> ToModelConverterExpression =
                p =>
                new BlogPostModel
                {
                    Title = p.Title,
                    Authors = p.Authors.AsQueryable().Select(UserModelExtensions.ToModelConverterExpression),
                };
    
            public static readonly Func<BlogPost, BlogPostModel> ToModelConverterFunction = ToModelConverterExpression.Compile();
    
            public static IQueryable<BlogPostModel> ToModel(this IQueryable<BlogPost> blogPosts)
            {
                return blogPosts.Select(ToModelConverterExpression);
            }
    
            public static IEnumerable<BlogPostModel> ToModel(this IEnumerable<BlogPost> blogPosts)
            {
                return blogPosts.Select(ToModelConverterFunction);
            }
        }
    
        public static class UserModelExtensions
        {
            public static readonly Expression<Func<User, UserModel>> ToModelConverterExpression =
                u =>
                new UserModel
                {
                    Name = u.Name,
                    Email = u.Email,
                };
    
            public static readonly Func<User, UserModel> ToModelConverterFunction = ToModelConverterExpression.Compile();
    
            public static IQueryable<UserModel> ToModel(this IQueryable<User> users)
            {
                return users.Select(ToModelConverterExpression);
            }
    
            public static IEnumerable<UserModel> ToModel(this IEnumerable<User> users)
            {
                return users.Select(ToModelConverterFunction);
            }
        }
    }
    

    To test it without actually creating a database:

    var blogPostsQuery = (
        from p in context.BlogPosts
        where p.Title.StartsWith("a")
        select p).ToModel();
    Console.WriteLine(((ObjectQuery)blogPostQuery).ToTraceString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application which uses plain HTML. I quickly need to
We have an ASP.NET application on ASP.NET 4.0 using MVC 3 which uses Windows
I have an asp.net mvc application, which uses the default user database. It all
I have an MVC 3 application which uses asp.net authentication. I have just created
I have an Asp.net MVC application which uses html5 and jquery on the client
I have an ASP.NET MVC 4 application which uses the new ASP.NET Web API.
I have a ASP.NET MVC application which is build up as an assembly that
I have an ASP.NET MVC 3 application which has a post action called Create
I have an .NET 4.0 ASP.NET MVC application, which also hosts a Workflow Foundation
In the asp.net mvc 3 application I have two views which have the same

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.