Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance difference with MemberInit Expression
    primarykey
    data
    text
    <p>I am working a similar problem as <a href="https://stackoverflow.com/questions/222511/refactor-to-eliminate-repetition-in-lamba-expression">Question 222511</a> I do need to use the MemberInit Expression so I can just add them to the constructor... I am trying to implement <a href="https://stackoverflow.com/users/22656/jon-skeet">John Skeet's</a> answer but I am running into a big performance difference. Here is some of the code:</p> <pre><code>// Method A: // This work good, is fast and returns an un-executed query... DataContext.LoanNote.Join&lt;LoanNote, Customer, int, LoanNote&gt;( DataContext.Customers, loanNote =&gt; loanNote.PrimaryCustomerNumber, customer =&gt; customer.CustomerNumber, (LoanNote loanNote, Customer customer) =&gt; new LoanNote() { AccountFeeBillAmount = loanNote.AccountFeeBillAmount, AccountOpenDate = loanNote.AccountOpenDate, // This goes on and on... PrimaryCustomer = customer }); // Method B: // This on the other hand is a lot slower and I am not sure why... var resultSelector = BuildJoinResultSelector&lt;LoanNote, Customer, LoanNote("PrimaryCustomer").Compile(); DataContext.LoanNote.Join&lt;LoanNote, Customer, int, LoanNote&gt;( DataContext.Customers, loanNote =&gt; loanNote.PrimaryCustomerNumber, customer =&gt; customer.CustomerNumber, resultSelector); // The build MemberInitExpression method... private static Expression&lt;Func&lt;TOuter, TInner, TResult&gt;&gt; BuildJoinResultSelector&lt;TOuter, TInner, TResult&gt;(string propertyName) where TResult : class { var result = default(Expression&lt;Func&lt;TOuter, TInner, TResult&gt;&gt;); var resultType = typeof(TResult); var outerType = typeof(TOuter); var innerType = typeof(TInner); var outer = Expression.Parameter(outerType, "outer"); var inner = Expression.Parameter(innerType, "inner"); var bindings = new List&lt;MemberBinding&gt;(); foreach (var property in resultType.GetProperties()) { if (property.CanRead == false) { continue; } else if (property.CanWrite == false) { continue; } else if (property.Name == propertyName) { var condition = Expression.Condition(Expression.Equal(inner, Expression.Constant(null)), Expression.New(innerType), inner); bindings.Add(Expression.Bind(property, condition)); } else { bindings.Add(Expression.Bind(property, Expression.Property(outer, property))); } } var memberInit = Expression.MemberInit(Expression.New(resultType), bindings); result = Expression.Lambda&lt;Func&lt;TOuter, TInner, TResult&gt;&gt;(memberInit, outer, inner); return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload