score:1
Can't you just use automapper for that?
public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary()
{
return m => Mapper.Map<TblCustomer, CustommerSummary>(m);
}
You'd have to do some bootstrapping, but then it's very reusable.
UPDATE:
I may not be getting something, but what it the purpose of this function? If you just want to map one or collection of Tbl object to other objects, why have the expression?
You could just have something like this:
var customers = _customerRepository.GetAll(); // returns IEnumerable<TblCustomer>
var summaries = Mapper.Map<IEnumerable<TblCustomer>, IEnumerable<CustomerSummary>>(customers);
Or is there something I missed?
score:0
I don't think you'll be able to use a lambda expression to do this... you'll need to build up the expression tree by hand using the factory methods in Expression
. It's unlikely to be pleasant, to be honest.
My generally preferred way of working out how to build up expression trees is to start with a simple example of what you want to do written as a lambda expression, and then decompile it. That should show you how the expression tree is built - although the C# compiler gets to use the metadata associated with properties more easily than we can (we have to use Type.GetProperty
).
This is always assuming I've understood you correctly... it's quite possible that I haven't.
score:0
How about this:
public static Person CreatePerson(TblPerson data)
{
// ...
}
public static Expression<Func<TblPerson, Person>> CreatePersonExpression()
{
return d => CreatePerson(d);
}
return m => (new CustomerSummary()
{
ID = m.ID,
CustomerName = m.CustomerName,
LastSalesContact = CreatePerson(m.LatestPerson)
});
Source: stackoverflow.com
Related Query
- How can I create an Expression within another Expression?
- How do I create an Expression out of another Expression in C#?
- How can I create a LINQ-friendly 'return false' expression using C# expression trees?
- How can I create a dynamic select expression in LINQ?
- How can I compare two Lists and create another list where the match?
- How can I, using the C# lambda expression syntax, invoke another expression?
- How to create an Expression that invokes (or is combined with) another Expression using a closure object as argument?
- using linq, how can i create a IEnumerable<> from a property of another IEnumerable<>
- How can I create SQL that does a Left Outer Join and also a count from another table?
- How can I create a JSONPath filter expression containing both AND and OR operators?
- How can I take a list of dates, and remove any that are within 1 minute of another date in the list with LINQ?
- How can I create a LINQ expression that joins four tables and allows a where on the topmost table?
- How can I create a Linq AND expression in C#?
- How can I create an Lambda Expression for Linq in the Where clause for two tables after the Join?
- How can I create a JSONPath filter expression to search using contains keyword
- How do I find the text within a div in the source of a web page using C#
- Can you create a simple 'EqualityComparer<T>' using a lambda expression
- How to create LINQ Expression Tree to select an anonymous type
- How do I create an expression tree calling IEnumerable<TSource>.Any(...)?
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- How can I reuse expressions within LINQ statements?
- How do I create a Linq expression tree with an F# lambda?
- In C#, how can I filter a list using a StartsWith() condition of another list?
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- How can I debug or set a break statement inside a compiled expression tree?
- How can I Create a Expression.Property of a child object
- How can I create a conditional where clause using LINQ
- How can I create an Action<T> in F#?
- How can you update a Linq Expression with additional parameters?
- How can I create a dynamic Select on an IEnumerable<T> at runtime?
More Query from same tag
- How to use a partial class to set an associations child property to false
- Complex merge of one List<CustomType> into another
- Outputting items in a collection in groups
- C#/LINQ Taking Sum of Decimals show different results
- finding possible combinations linq
- Nesting list within a list that are different types
- Find closest and smaller value in a list in C# with linq?
- Fetching timezone based on user logged in with httpcontext asp.net mvc
- Getting error while parsing json response from a dynamic {System.RuntimeType} variable
- How scalable is LINQ?
- Trying to merge two string array based on comparision
- Recursive linq query
- Sorting list of list of bytes or list of byte arrays
- Hot/Cold Observable, 1 subscribers with multiple select query?
- How do I display 2 foreign keys in a view ASP.NET MVC by using LinQ
- How do I modify a collection after being calculated?
- How can i enable Transaction my codes with linqto SQL?
- linq join with single
- Linq vs Lambda-Expressions query execution and "1 AS [C1]" meening in executed query
- Cast Exception in C# Where clause
- Linq - check if the any of the values present in an array exists in the database table
- Subsonic Single WHERE clause
- Populating Textblock From LINQ Query (Web Service)
- Duplicate key error but no entry with same values to be found when inserting data through LINQ
- LINQ Multiple Sort Dynamically C#
- DataContext reusing connection
- C# AddRange List<List<T>>
- c# linq OrderByDescending with inner LastOrDefault
- Finding Unique Objects between collections
- Inefficient MVC ViewModel Making Multiple Calls to the Database?