score:1
Accepted answer
using Automapper version 5.2.0, your syntax should be as below. You should use ResolveUsing instead of MapFrom and swith-case should be within curly brackets. you dont have to use any Mapper.Initialize(). I tested this code and it works for me. I hope that it helps for you also. Cheers.
Similar question asked here. see the answer from Mrchief
var config = new MapperConfiguration(cfg =>
{
Mapper.CreateMap<Report, ReportViewModel>()
.ForMember(src => src.Value,
dest => dest.ResolveUsing(r => {
switch(reportingPeriod)
{
case ReportingPeriod.Daily:
return r.Day.Value;
break;
case ReportingPeriod.Weekly:
return r.Week.Value;
break;
case ReportingPeriod.Monthly:
return r.Month.Value;
break;
case default:
//None
return null;
break;
}
}));
});
var mapper = config.CreateMapper();
score:0
You need to return the value. IMO a switch statement is clearer than nested if else:
Mapper.CreateMap<Report, ReportViewModel>()
.ForMember(src => src.Value,
dest => dest.MapFrom(r =>
switch(reportingPeriod)
{
case ReportingPeriod.Daily:
return r.Day.Value;
break;
case ReportingPeriod.Weekly:
return r.Week.Value;
break;
case ReportingPeriod.Monthly:
return r.Month.Value;
break;
case default:
//None
return null;
break;
}));
Source: stackoverflow.com
Related Query
- Overwrite Existing Mapping with Automapper
- .Net Core API & Automapper with many to many relationship mapping
- What does this C# code with an "arrow" mean and how is it called?
- How to PLINQ an existing LINQ query with Joins?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- Why is this code with PredicateBuilder not working?
- querying existing ListView items with LINQ
- Can you advise me a resource with LINQ/lambda code exercises?
- LINQ Source Code Available
- Linq with where clause in many-to-many EF Code First object
- LINQ GroupBy Aggregation with AutoMapper
- How do I update a table with LINQ-to-SQL without having to delete all the existing records?
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Mapping collections with LINQ
- AutoMapper ProjectTo fails with FirstOrDefault
- C#: Adding Columns To Bound DatagridView With Code
- How to map Integer to String with AutoMapper 3 and Entity Framework
- AutoMapper ProjectTo: Not working with nested objects
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- BestPractice: Pros and Cons for using AutoMapper or LINQ (LINQ to Objects) for mapping between a Domain Model and a Presentation Model
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- compressing code with C# generics
- many to many mapping in entity framework code first
- How do I represent an option calculated column with EF Code First?
- How can I combine IObservable<T>.Throttle() with some other event source using Reactive Extensions?
- Linq data mapping with Ms Access database: "Missing semicolon (;) at end of SQL statement."
- creating Linq to sqlite dbml from DbLinq source code
- Stubbing Code for Test With Linq Expressions and Lambdas
More Query from same tag
- Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context
- How to pass value by reference in C#?
- Linq to XML if/foreach with XElement
- Refactor LINQ ForEach to return IEnumerable<T>
- Using LINQ vs SQL for Filtering Collection
- LINQ ile Array Splitting
- How to get a nested json result after grouping a list?
- Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported
- How to check if a property from an object in a List<T> exists in another List<T>?
- Multiple group by and Sum LINQ
- Why SubmitChanges doesn't work?
- How to extend LINQ select method in my own way
- How to get stored procedure return value using linq with C#?
- Add a user-defined scalar function as a property to a Linq-To-Sql class
- Linq query to update values based on another column
- Creating Dynamic Fetch Expression
- How to reduce dimension?
- Finding the difference between two lists of strings
- Can't save changes to database. Store update, insert, or delete statement affected an unexpected number of rows (0)
- How to use CopyToDataTable() method in LINQ successfully using below example only?
- Turn Linq to Entities Aggregate with seed around
- Linq to format a list of items to replace foreach loop
- select some ids from sql in linq
- Sorting dates already CHAR
- Cross Database Join with Linq - Updating T4 template to support DB name?
- Cannot fire itemcommand event asp.net
- LINQ Select only throws IOException when the Enumerable is looked at
- Null value in the result of a left outer join linq causes error
- MVC - Passing multiple data tables to a view
- Display content in Repeater