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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:43:34+00:00 2026-05-27T13:43:34+00:00

so I have found a few questions on SO (for example: Automapper mapping list

  • 0

so I have found a few questions on SO (for example: Automapper mapping list becomes 0) about automapper returning a list of 0 from a mapping, but none seem quite what I’m looking at.

I have two types:

public class DNSContract : BaseContract
{
    public int DoNotSolicitID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Zip4 { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string BusinessName { get; set; }
    public string Partner { get; set; }
    public string Origination { get; set; }
}

and

public DNS_Entity()
    {
        // set default values which can be expicity set if needed
        InsertDT = DateTime.Now;
        InsertDT = DateTime.Now;
       // InsertUserID = 999;
        Origination = "RDI";
    }

    public long DoNotSolicitID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Zip4 { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string BusinessName { get; set; }
    public string Partner { get; set; }
    public string Origination { get; set; }
    public Nullable<System.DateTime> InsertDT { get; set; }
    //public int InsertUserID { get; set; }
    public DateTime? UpdateDT { get; set; }
    //public int UpdateUserID { get; set; }
}

pretty easy there. Here’s a “quick map” method I’m using:

 public static TToType QuickMap<TFromType, TToType>(this TFromType fromObject, TToType toObject)
        where TFromType : class
        where TToType : class, new()
    {
        // Look for an existing map, and if none is found add one.
        if (Mapper.FindTypeMapFor(typeof (TFromType), typeof (TToType)) == null)
        {
            Mapper.CreateMap(typeof (TFromType), typeof (TToType));
        }

        // Execute the auto map
        TToType map = Mapper.Map(fromObject, toObject);

        return map;
    }

so far so good I guess. However,

TToType map = Mapper.Map(fromObject, toObject);

does nothing. The problem is that this code returns an empty DNSContract when it should return 4 (from my unit test):

 using (var scope = dnsWork)
        {
            scope.Register(this);

            var one = WhereInternal(whereClause);
            var two = one.ToList();
            var three = two.QuickMap(new List<DNSContract>());
            return three;
            //return WhereInternal(whereClause).ToList().QuickMap(new List<DNSContract>());
        }

I’ve broken the call out into one, two, three for sanity’s sake while debugging this. So basically, I have a List and want to return a List and that’s failing.

Something that does work is:

return Mapper.Map(two, new List<DNSContract>());

but I’d like to use the generic method and not have mapping sprinkled throughout the service layers.

With automapper do I need to do anything special for list mappings? I think that’s the problem I’ve mapping types but for some reason the list to typeB is just not working right.

Thanks. This has been annoying me for a couple of weeks now and sort of ignored it, but I need to fix is ASAP.

UPDATE #1: as requested, below is the WhereInternal method in a snippet of its class which is in my DAL and pulls from Entity Framework:

 public abstract class EFRepository<T> : IRepository<T> where T : BaseEntity
{
    public IUnitOfWork UnitOfWork { get; set; }

    private IDbSet<T> _objectset;

    private IDbSet<T> ObjectSet
    {
        get { return _objectset ?? (_objectset = UnitOfWork.Context.Set<T>()); }
    }

    public IQueryable<T> WhereInternal(Expression<Func<T, bool>> expression)
    {
        return ObjectSet.Where(expression);
    }
}

I don’t think it’s really important in the context, since I convert to a list and then attempt to map.

  • 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-27T13:43:35+00:00Added an answer on May 27, 2026 at 1:43 pm

    First, there is a problem in your implementation of QuickMap method – i’m not sure why you want a second argument to be passed at all if you actually just need a type of it. You also call not the most straightforward methods to perform the mappings.

    Second, as per documentation, register a mapping only for the simple types, hence I would separate registration and mapping itself.
    Here is what I came up with:

    static class MapperHelper
    {    
        static void Register<TSource, TDestination>()
        {
            var mapped = Mapper.FindTypeMapFor(typeof(TSource), typeof(TDestination));
            if (mapped == null)
            {
                var expression = Mapper.CreateMap<TSource, TDestination>();
            }
        }
        static TDestination QuickMap<TSource, TDestination>(this TSource source)
        {
             return Mapper.Map<TSource, TDestination>(source);
        }
    }
    

    And the usage:

    //Registration
    MapperHelper.Register<DNS_Entity, DNSContract>();
    //Mapping
    var result = WhereInternal(whereClause).ToList().QuickMap<IList<DNS_Entity>, IList<DNSContract>>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions about the linkage from the following variables. By examples
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
I have found a few good replies to similar content so far, but never
I've had a look around and I have found a few questions like mine
Questions about threads are in no shortage, I know, but I can't seem to
I have a few questions about the INTENT of variables within a subroutine in
I found the following semaphore example. I'm currently learning about thread syncing. I have
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found many tutorials about using Windows Server 2003 as a development machine,
I have found ASP.Net PageMethods very handy and easy to use, but I have

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.