score:1
Accepted answer
you can use the expression.increment
rather than doing a + 1.
or we can make it really generic.
something like this:
void main()
{
var mapperconfiguraiton =
new mapperconfiguration(cfg =>
cfg.createmap<widget, widgetmodel>()
.increment(x => x.countd, src => src.count)
.toupper(x => x.name, src=>src.name));
var widget = new widget {count = 3, name="jimmy"};
var mapper = mapperconfiguraiton.createmapper();
var model = mapper.map<widgetmodel>(widget);
}
public class widget {
public int count {get; set;}
public string name {get;set;}
}
public class widgetmodel {
public int count {get; set;}
public string name {get;set;}
}
public static class mapperextensions {
public static imappingexpression<tsource, tdestination> increment<tsource, tdestination>(this imappingexpression<tsource, tdestination> expression,
expression<func<tdestination, int>> destinationmember,
expression<func<tsource, int>> sourcemember)
{
return expression.customaction(destinationmember, sourcemember, s => s + 1);
}
public static imappingexpression<tsource, tdestination> toupper<tsource, tdestination>(this imappingexpression<tsource, tdestination> expression,
expression<func<tdestination, string>> destinationmember,
expression<func<tsource, string>> sourcemember)
{
return expression.customaction(destinationmember, sourcemember, s => s.toupper());
}
public static imappingexpression<tsource, tdestination> customaction<tsource, tdestination, tdestinationmember, tsourcemember>(
this imappingexpression<tsource, tdestination> expression,
expression<func<tdestination, tdestinationmember>> destinationmember,
expression<func<tsource, tsourcemember>> sourcemember,
expression<func<tsourcemember, tdestinationmember>> transform)
{
var sourcememberexpression = (memberexpression)sourcemember.body;
var sourceparameter = expression.parameter(typeof(tsource));
var expr = expression.invoke(transform,
expression
.makememberaccess(sourceparameter, sourcememberexpression.member));
var lambda = (expression<func<tsource,tsourcemember>>)
expression.lambda(expr, sourceparameter);
var newexpression = expression.formember(
destinationmember,
opts => opts.mapfrom(lambda));
return newexpression;
}
}
output:
widget.count = 3
widget.name = "jimmy"
model.count = 4
model.name = "jimmy"
Source: stackoverflow.com
Related Query
- C# Change Return Value of Linq Expression
- Using a linq or lambda expression in C# return a collection plus a single value
- How write a LINQ expression that will return the maximum value of a field?
- Linq extension. Change property value in source list
- LINQ function to return list but compiler says function doesn't return a value on all code path
- How can I get LINQ to return the object which has the max value for a given property?
- LINQ - Does the Where expression return new instance or reference to object instance
- LINQ Expression to return Property value?
- Get the parameter value from a Linq Expression
- Return null instead default value in LINQ
- How do you return a default value if a LINQ to entities query returns no values
- LINQ "Where" condition -> change value of property
- Linq expression to set all values of an array to a given value
- How to get a value out of a Span<T> with Linq expression trees?
- Default value for linq select item if query didn't return anything
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ to Entities - How to return a single string value from entity
- How to reuse a linq expression for 'Where' when using multiple source tables
- Get Value and Count of that value using LINQ or lambda expression
- Linq expression that would always return true
- How can I get LINQ to return the index of the object which has the max value in a collection?
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- LINQ Source Code Available
- How can I make my Linq select return values if the value selected is null?
- Why doesn't an async LINQ Select lambda require a return value
- Lambda expression to return one result for each distinct value in list
- Linq expression to find the max value of a List<List<int>>?
- how to change the value of dictionary using linq based on key?
- How to Deal With Dapper return Key Value pair with Dynamic from LINQ
- Linq expression to return the first overloaded method that takes 1 string parameter
More Query from same tag
- Clean way to return array indices matching a condition using LINQ
- Linq Groupby Sum a nested observable collection
- LINQ return a value IEnumerable
- "And" condition in C#/LINQ Query
- LINQ, SQLite, grouping and count
- Validation and Linq
- Is there a way I can cache a LINQ parameter and reuse it later?
- C#: Search a list of defined queryable fields, different per model, with linq through generics (using entity framework)
- SQL to LINQ Query with date in where clause is not working
- How can I use a Predicate<T> in an EF Where() clause?
- Why my ordering by linq does not work
- intersection of unknown number of lists c# .net
- How to access a particular data in LINQ query result?
- Select Clause In LINQ Query
- How to use index/position with Where in LINQ query language?
- Linq Query Where() with Date subtraction taking excessive time
- Getting the key value paired differences in two data rows?
- Cannot convert from IEnumerable<List> to List
- Can a method chain be called LINQ?
- Fill List<ViewModel> with strings from Regex.Matches
- Recursive query takes too much time to load data in C#
- Linq2Sql - Storing Complex Linq Queries for future dynamic execuction - raw text - possible?
- LINQ with grouping and summation
- Filtering IQueryable sub list
- Why my DataContext doesn't see the changes to an object(Unit Of Work)?
- Why is my mocked list not removing a certain element?
- InvalidCastException in a LINQ query
- Upgrade to 4.0 Framework, then downgraded, now can't find Linq namespace?
- convert an object list with list to a single list
- Sort DataRow[] using LINQ