Dynamic Expression.Call Any

public class Foo
    {
        public IList<string> Strings { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Func<Foo, bool> func =
            //    a => a.Strings.Any(b => b == "asdf");

            // b => b == "asdf";
            var bParameter = Expression.Parameter(typeof(string), "b");
            var asdfConstant = Expression.Constant("asdf");
            var compare = Expression.Equal(bParameter, asdfConstant);
            var compareExpression = Expression.Lambda<Func<string, bool>>(compare, bParameter);
            var ceCompareExpression = Expression.Constant(compareExpression.Compile());

            // a => a.Strings.Any(compareExpression)
            var parameter = Expression.Parameter(typeof(Foo), "foo");

            var foosProperty = Expression.Property(parameter, typeof(Foo).GetProperty("Strings"));
            MethodInfo method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(string));

            var anyMethod = Expression.Call(method, foosProperty, ceCompareExpression);

            var lambdaExpression = Expression.Lambda<Func<Foo, bool>>(anyMethod, parameter);

            // Test.
            var foo = new Foo { Strings = new List<string> { "asdf", "fdsas" } };

            //Console.WriteLine(string.Format("original func result: {0}", func(foo)));
            Console.Write(string.Format("constructed func result: {0}", lambdaExpression.Compile()(foo)));

            Console.ReadKey();
        }
    }
时间: 2024-10-11 07:52:19

Dynamic Expression.Call Any的相关文章

&quot;One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?&quot;的解决方法

#事故现场: 在一个.net 4.0 的项目中使用dynamic,示例代码如下: 1 private static void Main(string[] args) 2 { 3 dynamic obj; 4 obj = new { name = "jack" }; 5 Console.WriteLine(obj.name); 6 } 在读取obj.name时,报错: One or more types required to compile a dynamic expression c

One or more types required to compile a dynamic expression cannot be found.

This is because dynamic keyword is a new C# keyword. So we need to import Microsoft.CSharp.dll. Here is offical document: http://msdn.microsoft.com/en-us/library/dd264736.aspx  the very first sentence: " Visual C# 2010 introduces a new type, dynamic.

Entityframework 问题集锦

  作者:疯吻IT 出处:http://fengwenit.cnblogs.com   1. No Entity Framework provider found for the ADO.NET provider 方法很简单,添加下面的dll即可 EntityFramework.SqlServer.dll   2. Loading the include file 'EF.Utility.CS.ttinclude' returned a null or empty string After re

QML Object Attributes

Every QML object type has a defined set of attributes. Each instance of an object type is created with the set of attributes that have been defined for that object type. There are several different kinds of attributes which can be specified, which ar

SignalR Troubleshooting

This document contains the following sections. Calling methods between the client and server silently fails Configuring IIS websockets to ping/pong to detect a dead client Other connection issues Compilation and server-side errors Visual Studio issue

解读ASP.NET 5 &amp; MVC6系列(12):基于Lamda表达式的强类型Routing实现

原文:解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现 前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将讲述一种基于Lambda表达式的强类型类型. 这种方式的基本使用示例如下: services.Configure<MvcOptions>(opt => { opt.EnableTy

A real sense 3D face reconstruction system based on multi-view stereo vision

Abstract This paper proposed a system for a real sense 3D facial reconstruction method based on multi-view stereo vision generated using an orthographic 3D model. Multi-view stereopsis is an effective technology for expanding perspective and reducing

ef linq select where dynamic singleordefault

singleordefault(where) 条件不支持动态 所以想要达到目标,就需要转换思路,把where在前面调用,然后再接,代码如下 public TResult GetSingle<T, TResult>(Expression<Func<T, bool>> exWhere, Expression<Func<T, TResult>> selector) where T : class { using (SysDb<T> db =

ALinq Dynamic 使用指南——前言

一.简介 ALinq Dynamic 为ALinq以及Linq to SQL提供了一个Entiy SQL的查询接口,使得它们能够应用Entity SQL 进行数据的查询.它的原理是将Entiy SQL解释为Linq表达式,再执行生成的Linq表达式. 1.关于 Entity SQL Entity SQL是一种类似于SQL的语言,用于在Entity Framework中查询概念模型.概念模型将数据表示为实体和关系,而Entity SQL允许您以那些用过SQL的人熟悉的格式查询这些实体和关系.以上这