LINQ的Expression与delegate表达式
Linq的delegate表达式,Insus.NET觉得它封装得好,让开发时简化了很多代码,而且容易阅读与检索。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Linq.Expressions; namespace Insus.NET.Utilities { public class Class2 { public decimal Preferential(decimal amount) { return amount * (1 - 0.85m); //amount - amount * 0.85m; } } }
接下来,Insus.NET还想使用一个表达式树的类型Expression<TDelegate>实现:
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Linq.Expressions; namespace Insus.NET.Utilities { public delegate decimal pref(decimal amount); public class Class2 { //public decimal Preferential(decimal amount) //{ // return amount * (1 - 0.85m); //amount - amount * 0.85m; //} // public pref Preferential11 = a => (a * (1 - 0.85m)); static Expression<pref> expr = amount => (amount - amount * 0.85m); public pref Preferential2 = expr.Compile(); } }