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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:20:48+00:00 2026-06-16T05:20:48+00:00

I’m trying to auto project from SQL server with Automapper some data into my

  • 0

I’m trying to auto project from SQL server with Automapper some data into my view models.

The view model I have is:

public sealed class WorkstationViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string OccupiedByName { get; set; }
}

And the code I’m trying to use is:

Mapper.CreateMap<Workstation, WorkstationViewModel>()
  .ForMember(T => T.OccupiedByName, T => T.MapFrom(W =>
    W.Sessions.AsQueryable().Select(E => E.StaffMember.Name).SingleOrDefault()));

Two properties Id and Name are auto-projected as they have equal names in Workstation class.

The exception I get on some codelines like this

var model = WorkstationsRepository.GetAll().Project()
           .To<WorkstationViewModel>().SingleOrDefault();

is some weird object reference is null exception and on the top of the stack trace there are some automapper’s CreateExpression<> methods which gives me a conclusion that the automapper cannot generate a good one expression to translate it to SQL code.

When I use simple maps, like .Name to .Category.Name or other one-item retrievals from the SQL table, it works perfectly. All I need is to get access to multiple items while projecting the sequence via Automapper.

  • 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-16T05:20:49+00:00Added an answer on June 16, 2026 at 5:20 am

    The newer Project().To() API takes a totally different route than the “classic” Mapper.Map() API. I think that the latter would run in your case, but of course you won’t have the benefit of the projection trickling thought to the SQL.

    During Project().To() AutoMapper tries to get MemberInfos (reflection) from the involved types, which it uses to create lambda expressions. This means that the source properties in a mapping must be members of the source type. Evidently, W.Sessions.AsQueryable().Select(... is not a member of Workstation. So somewhere along the line AutoMapper bumps into a null memberinfo.

    So, Project().To() is a bit restricted. In this particular case a remedy may be to map a Session with its parents WorkStation and StaffMember to the model. Reference navigation properties will map OK with Project().To().

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

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.