C#之Expression表達(dá)式樹實(shí)例
本文實(shí)例講述了C#之Expression表達(dá)式樹,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
表達(dá)式樹表示樹狀數(shù)據(jù)結(jié)構(gòu)的代碼,樹狀結(jié)構(gòu)中的每個(gè)節(jié)點(diǎn)都是一個(gè)表達(dá)式,例如一個(gè)方法調(diào)用或類似 x < y 的二元運(yùn)算
1.利用 Lambda 表達(dá)式創(chuàng)建表達(dá)式樹
2.編譯表達(dá)式樹,該方法將表達(dá)式樹表示的代碼編譯成一個(gè)可執(zhí)行委托
3.IQueryable<T>的擴(kuò)展方法,WhereIn的實(shí)現(xiàn)
完整代碼如下:
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace MongoDBTest
{
class Program
{
static void Main(string[] args)
{
//使用LambdaExpression構(gòu)建表達(dá)式樹
Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
Console.WriteLine(expr.Compile()(1, 2, 3));
//使用LambdaExpression構(gòu)建可執(zhí)行的代碼
Func<int, int, int, int> fun = (x, y, z) => (x + y) / z;
Console.WriteLine(fun(1, 2, 3));
//動(dòng)態(tài)構(gòu)建表達(dá)式樹
ParameterExpression pe1 = Expression.Parameter(typeof(int), "x");
ParameterExpression pe2 = Expression.Parameter(typeof(int), "y");
ParameterExpression pe3 = Expression.Parameter(typeof(int), "z");
var body = Expression.Divide(Expression.Add(pe1, pe2), pe3);
var w = Expression.Lambda<Func<int, int, int, int>>(body, new ParameterExpression[] { pe1, pe2, pe3 });
Console.WriteLine(w.Compile()(1, 2, 3));
List<Entity> list = new List<Entity> { new Entity { Id1 = 1 }, new Entity { Id1 = 2 }, new Entity { Id1 = 3 } };
var d = list.AsQueryable().WhereIn(o => o.Id1, new int[] { 1, 2 });
d.ToList().ForEach(o =>
{
Console.WriteLine(o.Id1);
});
Console.ReadKey();
}
}
public class Entity
{
public ObjectId Id;
public int Id1;
public string Name { get; set; }
}
public static class cc
{
public static IQueryable<T> WhereIn<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> obj, IEnumerable<TValue> values)
{
return query.Where(BuildContainsExpression(obj, values));
}
private static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector)
{
throw new ArgumentNullException("valueSelector");
}
if (null == values)
{
throw new ArgumentNullException("values");
}
var p = valueSelector.Parameters.Single();
if (!values.Any()) return e => false;
var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate(Expression.Or);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
}
}
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)快遞api接口調(diào)用方法
這篇文章主要介紹了C#實(shí)現(xiàn)快遞api接口調(diào)用方法,主要是通過快遞API網(wǎng)接口的服務(wù),使用的時(shí)候直接申請個(gè)接口UID即可,有需要的小伙伴來參考下吧。2015-03-03c#中SAPI使用總結(jié)——SpVoice的使用方法
最近使用C#重做了點(diǎn)名系統(tǒng)(要用到TTS,讓計(jì)算機(jī)點(diǎn)名)使用了SAPI,在這里總結(jié)一下SpVoice的使用方法。2011-10-10C#多線程學(xué)習(xí)之(一)多線程的相關(guān)概念分析
這篇文章主要介紹了C#多線程學(xué)習(xí)之多線程的相關(guān)概念,涉及C#中多線程的相關(guān)概念與使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04