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

  final.Dump();
}

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>>(
        Expression.MemberInit(
            Expression.New(typeof(TDestination).GetConstructor(Type.EmptyTypes)),
            from selector in selectors
            let replace = new ParameterReplaceVisitor(
                  selector.Parameters[0], param)
            from binding in ((MemberInitExpression)selector.Body).Bindings
                  .OfType<MemberAssignment>()
            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;
        this.to = 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 http://pills2sale.com/ viagra online

  • Cyril

    Hold the line, please <a href="https://www.ryanmcbain.com/forum/ranitidine-tablets-for-dogs-2yfb">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="http://adena.ro/v2/cms-open-payments-data-dictionary-v3ei">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="https://log.akosut.com/zyprexa-classification-jdrk">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="http://thewhitedogbistro.com/finasterida-ultrafarma-ulzw">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="http://magnocentrojoyero.com/blog/iqos-cigarette-price-ph-utpp">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.

发表评论

博客统计

访问量:5263828

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

原创126 翻译20 转载604