Lambda Expressions (Expression<Func<T, T>>)组合demo

Lambda Expressions (Expression<Func<T, T>>)组合demo

void Main()
  var final = UpdateBulk((Problem p) => new Problem{CatId = 1,SubCatId = 2, ListId=3});

 // final is of type Expression<Func<T,T>>, which can be used for further processing


public static Expression<Func<T, T>> UpdateBulk<T>(Expression<Func<T, T>> updateFactory) where T : IBaseEntity, new()
    Expression<Func<T, T>> modifiedExpression = x => new T() { ModifiedBy = "Test", ModifiedDate = DateTime.Now };

    var result = Combine(updateFactory, modifiedExpression);

    return result;

static Expression<Func<TSource, TDestination>> Combine<TSource, TDestination>(
    params Expression<Func<TSource, TDestination>>[] selectors)
    var param = Expression.Parameter(typeof(TSource), "x");
    return Expression.Lambda<Func<TSource, TDestination>>(
            from selector in selectors
            let replace = new ParameterReplaceVisitor(
                  selector.Parameters[0], param)
            from binding in ((MemberInitExpression)selector.Body).Bindings
            select Expression.Bind(binding.Member,
                  replace.VisitAndConvert(binding.Expression, "Combine")))
        , param);

class ParameterReplaceVisitor : ExpressionVisitor
    private readonly ParameterExpression from, to;
    public ParameterReplaceVisitor(ParameterExpression from, ParameterExpression to)
        this.from = from; = to;
    protected override Expression VisitParameter(ParameterExpression node)
        return node == from ? to : base.VisitParameter(node);

public abstract class IBaseEntity
    public System.DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }

    public System.DateTime ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }

    public string DeletedBy { get; set; }

public class Problem : IBaseEntity
    public int CatId { get; set; }

    public int SubCatId { get; set; }

    public int ListId { get; set; }



  • Merziuz

    40lxIe viagra online

  • Cyril

    Hold the line, please <a href="">kegunaan ranitidine</a> The City University of New York and Ret. Gen. David Petraeus have agreed: After a public relations stink, the estimable general and former CIA chief will teach a class at CUNY’s Macaulay Honors College for a $149,999 discount on the sizable $150,000 he was initially to be paid.

  • Lester

    What do you like doing in your spare time? <a href="">open payments data dictionary </a> Smartphones are increasingly becoming the target of hacking attempts, as people store more and more data on their devices, but there is one part of the phone that was always thought to be un-hackable &ndash; the SIM card.

  • Ariel

    Very interesting tale <a href="">zyprexa drug classification</a> After inking a contract for nearly $10,000 to pay for the tear-down out of his pocket, Sutton and his wife, Sue, watched as heavy equipment smashed it to bits in less than 40 minutes. Sue fought back some tears but held it together as the house came apart.

  • Deshawn

    Could I have , please? <a href="">finasteride lowest price</a> iGene isn't the first to run a scanner over a corpse. Radiology has been used on skulls for 30 years, and Israel first introduced the concept of a virtual autopsy in 1994. The U.S. military started conducting CT scans of all soldiers killed in Iraq and Afghanistan in 2004 in addition to traditional autopsies.

  • Marquis

    I have my own business <a href="">iqos 2.4 plus bluetooth ios</a> The ad campaign will roll out during the fall, Howard said, so the information is fresh in consumers' minds as enrollment begins in October. Along with television, newspaper and website ads, it will include social media campaigns on Facebook, Google and Twitter, he said.




博文总数:750 评论总数:910099

原创126 翻译20 转载604