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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:31:47+00:00 2026-05-21T05:31:47+00:00

I am trying to map information about the User to several dto’s, but I’m

  • 0

I am trying to map information about the User to several dto’s, but I’m getting null exceptions. Basically, the reason why I distributed the information among several classes is because there are common info that I need in several views. So this is what I ended up with:

User entity class

public class User 
{
    public int Id { get; set; }
    public string Nickname { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public float Credits { get; set; }
    public float PromotionalCredits { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public double RatingAverage { get; set; }
    public string ProfileImage { get; set; } 
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Item> Items { get; set; }
    public virtual ICollection<Bid> Bids { get; set; }
    public virtual ICollection<CreditCard> CreditCard { get; set; }
    public virtual ICollection<Message> ReceivedMessages { get; set; }
    public virtual ICollection<Message> SentMessages { get; set; }
    public virtual ICollection<Item> WatchList { get; set; }
    public virtual ICollection<Rating> OwnRatings { get; set; }
    public virtual ICollection<Rating> RatingsForOthers { get; set; }
 }

DTO’s and ViewModel calsses

public class UserInfoSummaryViewModel
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public UserDetailedStatus DetailedStatus { get; set; }
}

public class UserDetailedStatus
{
    public float TotalCredits { get; set; }
    public float Credits { get; set; }
    public float PromotionalCredits { get; set; }
    public BiddingAndItems BiddingAndItems { get; set; }
    public int OngoingListings { get; set; }
    public int NewMessages { get; set; }
    public float Rating { get; set; }
    public int NumberOfRatings { get; set; }
}

public class BiddingAndItems
{
    public int TotalBids { get; set; }
    public int WinningBids { get; set; }
    public int AcquiredItems { get; set; }
    public int ItemsAwaitingConfirmation { get; set; }
    public List<ItemForUserBids> Items { get; set; }
}

Mappings inside AutoMapperBootStrapper class

Mapper.CreateMap<User, BiddingAndItems>()
                .ForMember(m => m.TotalBids, o => o.MapFrom(s => s.TotalActiveBids()))
                .ForMember(m=>m.ItemsAwaitingConfirmation, o=>o.MapFrom(s=>s.Items.Count(i=>i.IsAwaitingReceptionConfirmation().Equals(true))))
                .ForMember(m=>m.AcquiredItems, o=>o.MapFrom(s=>s.AquiredItems().Count))
                .ForMember(m => m.WinningBids,
                           o => o.MapFrom(s => s.Bids.Where(c => c.Item.CurrentHighestBidderId().Equals(s.Id))));

            Mapper.CreateMap<User, UserDetailedStatus>()
                .ForMember(m => m.NumberOfRatings, o => o.MapFrom(s => s.OwnRatings.Count()))
                .ForMember(m => m.NewMessages, o => o.MapFrom(s => s.TotalUnreadMessages()))
                .ForMember(m => m.OngoingListings, o => o.MapFrom(s => s.Items.Where(i => i.IsPublished())))
                .ForMember(m => m.Rating, o => o.MapFrom(s => s.RatingAverage))
                .ForMember(m => m.TotalCredits, o => o.MapFrom(s => s.TotalCredits()));

            Mapper.CreateMap<User, UserInfoSummaryViewModel>();

Call to automapper inside UserController

    public ActionResult Summary()
    {

        var user = _helper.GetUserFromSession();
        var viewModel = Mapper.Map<User, UserInfoSummaryViewModel>(user);
        return View(viewModel);
    }

I thought because I have all the necessary mappings inside the bootstrapper this should, theoretically work, apparently I was wrong… How can I fix that?

UPDATE:

I got my mappings fixed and added a couple of value resolvers. Now I’m not getting a null reference exception but there seems to be something wrong because everytime I run the project it gets stuck and then the local server stops responding… Here’s my code:

        Mapper.CreateMap<User, BiddingAndItems>()
            .ForMember(m => m.TotalBids, o => o.MapFrom(s => s.TotalActiveBids()))
            .ForMember(m => m.ItemsAwaitingConfirmation,
                       o => o.MapFrom(s => s.Items.Count(i => i.IsAwaitingReceptionConfirmation().Equals(true))))
            .ForMember(m => m.AcquiredItems, o => o.MapFrom(s => s.AquiredItems().Count))
            .ForMember(m => m.WinningBids, o => o.ResolveUsing<WinningBidsResolver>())
            .ForMember(m => m.Items, o => o.ResolveUsing<BiddingItemResolver>());

        Mapper.CreateMap<User, UserDetailedStatus>()
            .ForMember(m => m.NumberOfRatings, o => o.MapFrom(s => s.OwnRatings.Count()))
            .ForMember(m => m.NewMessages, o => o.MapFrom(s => s.TotalUnreadMessages()))
            .ForMember(m => m.OngoingListings, o => o.MapFrom(s => s.Items.Where(i => i.IsPublished()).Count()))
            .ForMember(m => m.Rating, o => o.MapFrom(s => s.RatingAverage))
            .ForMember(m=>m.BiddingAndItems, o => o.MapFrom(s=> s))
            .ForMember(m => m.TotalCredits, o => o.MapFrom(s => s.TotalCredits()));

        Mapper.CreateMap<User, UserInfoSummaryViewModel>()
            .ForMember(m => m.DetailedStatus, o => o.MapFrom(s => s));

public class BiddingItemResolver : ValueResolver<User, List<ItemForUserBids>>
{
    protected override List<ItemForUserBids> ResolveCore(User source)
    {
        var items = new List<ItemForUserBids>();
        foreach (var bid in source.Bids)
        {
            var item = bid.Item;
            var c = new ItemForUserBids
                        {
                            BidValue = bid.Amount,
                            Description = item.Description,
                            Id = item.Id,
                            ItemThumb = item.MainImageLink(),
                            Status = source.ItemBiddingStatus(item.Id),
                            TimeLeft = TimeUtility.TimeLeft(item.EndDate),
                            Title = item.Title
                        };
            items.Add(c);
        }
        return items;
    }
}

public class WinningBidsResolver : ValueResolver<User, int>
{
    protected override int ResolveCore(User source)
    {
        return source.Bids.Where(c => c.Item.CurrentHighestBidderId().Equals(source.Id)).Count();
    }
}

The problem is that I’m not getting any exceptions to give me any hints about what’s going wrong… It just gets stuck! I suspect that my mappings are going into some sort of an infinite loops or something, but I am not sure what is happening exactly… Is there any way I could debug this problem?

Any help would be appreciated…

  • 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-21T05:31:47+00:00Added an answer on May 21, 2026 at 5:31 am

    Without more information about the exception you receive I can only guess what could go wrong: I guess it’s because you’re using Linq on uninitialized collections in MapFrom. Try implementing a ValueResolver instead.

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

Sidebar

Related Questions

I am trying to dynamically map JSON information into different objects. But I can't
I've been trying to map an object to a database using SQLAlchemy but have
I'm trying to make a Q3BSP map information debugger. I'm stuck at texture debugger
I am trying to map several tables using Fluent Nhibernate. My tests are giving
I'm trying to create a map using JVectorMap that will put information to an
Trying to map the following schema using the Entity Framework. A Customer can have
Im trying to map urls to numbers in a range [0,50] for porting, that
I'm trying to map a Dictionary containing Lists. I have the following set of
I am trying to map a legacy database here and I'm running into a
I am trying to map a different texture on each side of a cube

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.