score:20
Accepted answer
try this:
mapper.createmap<i_news, newsmodel>().formember(x => x.datecreated,
opt => opt.mapfrom(src => ((datetime)src.datecreated).toshortdatestring()));
score:0
if nullable is destination then:
mapper.createmap()
.formember(
dest => dest.startdate,
opt => opt.mapfrom(
src => string.isnullorempty(src.startdate) ? new datetime?() : datetime.parseexact(src.startdate, dateformat, cultureinfo.invariantculture)
)
)
score:24
in order to use lambda bodies, use .resolveusing
instead of .mapfrom
.
as per the author:
mapfrom has some extra stuff that needs expression trees (like null checking etc).
so your statement would look like this:
mapper.createmap<i_news, newsmodel>()
.formember(x => x.datecreated, opt => opt.resolveusing(src => {
var dt = (datetime)src.datecreated;
return dt.toshortdatestring();
}));
Source: stackoverflow.com
Related Query
- AutoMapper and convert a datetime to string
- C# Check datetime list whether contains linked dates and convert to string
- In LINQ Query convert the string to datetime and check with today's date
- How to convert string to datetime and then get the lowest date
- Convert datetime to a formatted string inside a LINQ-to-entities query
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- LINQ Query to Convert string to datetime
- How can I convert DateTime to String in Linq Query?
- convert string to DateTime in linq query with where clause?
- LINQ convert DateTime to string
- Dynamic LINQ - Unable to convert between GUID and string
- How to convert string to DateTime in C# EF Core query
- How to map Integer to String with AutoMapper 3 and Entity Framework
- Convert String to DateTime in LINQ query with Entity Framework Core
- Operator >= cannot be applied to operands of type string and datetime
- Code Example for Add DateTime and TimeSpan in EF query
- Convert string to long type and use in a linq query within asp.net MVC
- convert a string to datetime in LinqToEntities (inside the query)
- How to convert List to a string and back
- LINQ - Convert to String LIST and Replace Characters
- LINQ Convert string to datetime
- Convert delimited string to array and group using LINQ in C#
- InvalidCastException convert string to DateTime
- Concatenate Datetime and string and bind result to dropdownlist using Json method
- Convert string to DateTime using Entity Framework
- Convert comma separated string to int list and validate
- Split a string and convert to custom type
- LINQ - Convert String to Datetime
- how to convert string into DateTime in where Clause?
- how do i convert Dictionary string string to array of key value pair of string and string using linq and c#
More Query from same tag
- LINQ Select Statement. Anonymous Method Returns Exception
- Using a custom type in a LINQ query
- Excluding result based on length of a field
- From Eric Lippert's blog: "don't close over the loop variable"
- Taking distinct item from list of datapoints
- Return first x items in a group of groups
- sending multidimensional objects in c# mvc, and rendering individual attributes, to c# mvc view
- wrap a linq expression with another linq expression
- Linq Query to match string values in an xml
- Find underlying tables of views using Linq to Entity to make Aggregate Dependencies
- Filter entities, then sort on dynamic field
- Linq Select that will generate tied scores for similar values
- LINQ: Not Any vs All Don't
- The Big O of Distinct() method with a Custom IEqualityComparer
- Simplest way to filter value from generic List in C# using LINQ
- Skip ThenBy on nullable objects
- C# linq query rank by two conditions
- Dividing up a table of two-way relationships into distinct groups
- Linq Select all fields of inherited class and some fields of derived class
- C# LINQ: remove null values from an array and return as not nullable
- can linq expression be case insensitive
- AsParallel() executing sequentially
- What's the LINQ'ish way to do this
- Drill down filtering query in linq c#
- linq query with count
- Lambda expression not return correct result when using All()
- Entity Framework converts StartsWith to MySQL's Locate, MySQL's Locate doesn't use index
- Getting a list of objects of 2 different lists
- Why won't this Linq compile?
- LINQ: Compare two lists and count subset