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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:23:15+00:00 2026-05-28T14:23:15+00:00

Background ArticleService is a class that provides methods for front-end layers to facilitate business

  • 0

Background

ArticleService is a class that provides methods for front-end layers to facilitate business with the back-end.

Two of its base responsibilities are to convert ViewModels (ArticleViewModel) to the appropriate Models (Article) when persisting data, and the reverse, convert Models to ViewModels when fetching data… so often that I created a private method building ViewModel objects:

private ArticleViewModel BuildViewModel(Article a)
{
    return new ArticleViewModel { Title = a.Title /* all properties */ }
}

Moving along, the ArticleService provides a method to fetch all articles from the data store, returning them as ViewModels: public IEnumerable<ArticleViewModel> All()

A calling class uses it like this: var articleViewModels = _articleService.All();

Simple, right?

Problem:

I originally wrote All() lazily with a classic foreach loop:

private IEnumerable<ArticleViewModel> All()
{
    var viewModels = new List<ArticleViewModel>();
    foreach (var article in _db.Articles)
        viewModels.Add(BuildViewModel(article));
    return viewModels;
}

It worked fine – articleViewModels was an instantiate list of all view models.

Next, I used ReSharper to convert this loop to a LINQ statement for performance and prettiness, and then combined the assignment statement with the return statement. Result:

private IEnumerable<ArticleViewModel> All()
{
    return _db.Articles.Select(article => BuildViewModel(article)).ToList();
}

I debugged the LINQ statement and the beast awakened:

LINQ to Entities does not recognize the method ‘ArticleViewModel
BuildViewModel(Article)’ and this method cannot be translated into a store expression.

Question – Why does this LINQ statement break my code?

Note: Stepping back to an explicit declaration, assignment, return worked with the LINQ statement, so I’m almost certain it’s something to do with the lambda logic.

  • 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-28T14:23:15+00:00Added an answer on May 28, 2026 at 2:23 pm

    Question – Why does this LINQ statement break my code?

    Because LINQ to Entities is trying to translate BuildViewModel into SQL. It doesn’t know how, so it dies miserably.

    In your original version, you were streaming the entity from the database to your local box, and then doing the projection using BuildViewModel client-side. That’s fine.

    so I’m almost certain it’s something to do with the lambda logic.

    Nope. It’s because LINQ to Entities can’t translate BuildViewModel into SQL. It doesn’t matter if you use a lambda expression to express the projection or not.

    You can rewrite the code like this:

    return _db.Articles.
              .AsEnumerable()
              .Select(article => BuildViewModel(article)).ToList();
    

    This causes _db.Articles to be treated as a plain old enumerable, and then does the projection client side. Now LINQ to Entities doesn’t have to figure out what to do with BuildViewModel.

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

Sidebar

Related Questions

Background: At my company we are developing a bunch applications that are using the
Background I am trying to create a copy of a business object I have
Background: I have a css and a js that is used only by the
Background: I've wrote a small library that is able to create asp.net controls from
Background I've got an NSOutlineView that shows TrainingGroup entities. Each TrainingGroup represents a folder
Background This is only my second PyQt4 project. Developing a Windows app that has
Background I have a basic HTML page with an iframe that points to a
Background My project is urgent and requires that I iterate a large XML file
Background: My coworker and I are maintaining a million-line legacy application we inherited. Its
Background: My employeer at my non-programming job knows that I am an undergraduate CS

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.