automapper

Automapper - concrete object to array

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 15:23:03
I need to map some values from a class to an array. For example: public class Employee { public string name; public int age; public int cars; } must be converted to [age, cars] I tried with this var employee = new Employee() { name = "test", age = 20, cars = 1 }; int[] array = new int[] {}; Mapper.CreateMap<Employee, int[]>() .ForMember(x => x, options => { options.MapFrom(source => new[] { source.age, source.cars }); } ); Mapper.Map(employee, array); but i get this error: Using mapping configuration for Employee to System.Int32[] Exception of type 'AutoMapper.AutoMapperMappingException' was

AutoMapper isn't recognizing profile-specific prefixes

大憨熊 提交于 2019-12-04 14:35:43
I'm trying to use AutoMapper to take data from a class that has prefixes before property names and map it to a second class that doesn't have those prefixes. However, I don't necessarily want it to always strip out that prefix: I just want it to do it for this particular mapping. My source class looks like this: public class AdvancedSearchFilterDataModel { // .... public string ServiceMeterNumber { get; set; } // .... } My destination class looks like this: [DataContract] public class ServicesAdvancedSearchFilterData : AdvancedSearchFilterData { // .... [DataMember] public string MeterNumber {

Map Async Model Collection to Async ViewModel Collection

人走茶凉 提交于 2019-12-04 13:55:41
问题 I am working with a project where I need to work with Async programming C#. I am using Automapper for map between Model and ViewModel. For Async data I created a map method as follows: public static async Task<IEnumerable<PersonView>> ModelToViewModelCollectionAsync(this Task<IEnumerable<Person>> persons) { return await Mapper.Map<Task<IEnumerable<Person>>, Task<IEnumerable<PersonView>>>(persons); } And I called this mapping method as follows (In my service class): public async Task

How to Write xUnit Test for .net core 2.0 Service that uses AutoMapper and Dependency Injection? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-04 13:48:56
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I'm new to .net core/C# programming (coming over from Java) I have the following Service class the that uses dependency injection to get an AutoMapper object and a data repository object for use in creating a collection of SubmissionCategoryViewModel objects: public class SubmissionCategoryService :

Automapper, generics, dto funtimes

喜欢而已 提交于 2019-12-04 13:33:51
Here's the deal: I have a report designer where users can create reports based on some predefined datasets. They can select a set of columns to include in the report and then, when the report is ran, an IList is created by mapping the NHibernate collection over to the dto class collection using automapper. The problem with this is that the DTO collection has a load of redundant columns in as it will be populated with all the data regardless of whether or not it's needed. My solution to this? Why not create a DTO type at runtime, using the information we have and map the nhibernate collection

ASP.NET MVC ViewModel mapping with custom formatting

非 Y 不嫁゛ 提交于 2019-12-04 13:29:50
问题 The project I'm working on has a large number of currency properties in the domain model and I'm needing for format these as $#,###.## for transmitting to and from the view. I've had a view thoughts as to different approaches which could be used. One approach could be to format the values explicitly inside the view, as in "Pattern 1" from Steve Michelotti : ...but this starts violating DRY principle very quickly. The preferred approach appears to be to do the formatting during the mapping

AutoMapper for a list scenario only seems to repeat mapping the first object in the list

风格不统一 提交于 2019-12-04 13:18:29
I am developing an MVC 3 application and am using AutoMapper to move data between my ViewModels and my entities. I have a scenario where I need to move data between two lists. For some strange reason, AutoMapper seems to only copy the first object from the source object and then seems to copy the same object n times over to the destination list. For example, say you have 2 lists, source contains six entity items and destination contains 0 items as it was just instantiated. The item at position source[0] get copied over to the destination and then source[0] is copied repeatedly for the same

Automapper fails when projecting IQueryable<object> when object has a collection property

最后都变了- 提交于 2019-12-04 13:14:08
问题 So, here's my situation. I've got a my two classes: class FromClass { public string[] Foo { get; set; } } class ToClass { public string[] Foo { get; set; } } The classes have properties which are arrays. They could be List<T> or IEnumerable<T> , I get the same result in any of those cases. I try to map from one to the other using the AutoMapper.QueryableExtensions : class Program { static void Main(string[] args) { // create a "From" object string[] anArray = new string[] { "a", "b" };

How can I use a dataset in Automapper?

回眸只為那壹抹淺笑 提交于 2019-12-04 12:51:08
I am currently using a datareader as the source but I want to instead use a dataset. //datareader AutoMapper.Mapper.CreateMap<IDataReader, AccountDTO>() .ForMember(m => m.AccountId, opt => opt.MapFrom (r => r.GetInt32(r.GetOrdinal("AccountId")))) .ForMember(m => m.ParentAccountId, opt => opt.MapFrom(r => r.GetInt32(r.GetOrdinal("ParentAccountId")))) .ForMember(m => m.IsInactive, opt => opt.MapFrom(r => r.GetString(r.GetOrdinal("IsInactive")))) .ForMember(m => m.AccountName, opt => opt.MapFrom(r => r.GetString(r.GetOrdinal("AccountName")))) //dataset AutoMapper.Mapper.CreateMap<DataSet,

Flatten self referencing object in Auto Mapper

陌路散爱 提交于 2019-12-04 11:44:48
I have a self referencing model that I would like to convert to a flat list. The model looks like this. public class Node { public List<Node> Nodes { get; set; } public Person Person { get; set; } public Language Language { get; set; } } public class NodeDTO { public PersonDTO Person { get; set; } public LanguageDTO Language { get; set; } } public class NodeListDTO { public List<NodeDTO> Nodes { get; set; } } I want all nodes in the hierarchy to be flattend to one single list in my DTO object. Is this possible with Auto Mapper. I have tried to use a Custom Value resolver but I haven't figured