Tag: linq expressions

使用Expression <Func >以与Func 相同的方式编写调用

考虑一个可以用作多个其他类的成员的类: class Customer { public string FirstName {get;set;} public string LastName {get;set;} } // Both “Order” and “Profile” have a “Customer” property class Order { public Customer Customer {get;set;} } class Profile { public Customer Customer {get;set;} } 我想定义一个方法,为与Customer关联的对象生成检查器。 如果我想要一个内存检查器,我这样做: static Func Check(Func conv, string first, string last) { return obj => conv(obj).FirstName == first […]

如何构建IEnumerable .Contains()表达式?

我目前正在使用ASP动态数据,我正在尝试构建一个filter。 我们的用户需要根据项目是否是所选父项的子项来查找列表中的项目(我们的项目可以包含多个父项)。 有问题的项目是Segments,每个Segment都有一个名为RouteIds的属性,类型为IEnumerable,它是所有Segment的父ID的集合。 我已经在我的filter中覆盖了GetQueryable方法,但是我一直在显示的最后一行抛出exception: ConstantExpression ce = Expression.Constant(int.Parse(this.ddlRouteNames.SelectedValue)); ParameterExpression pe = Expression.Parameter(source.ElementType); MemberExpression me = Expression.Property(pe, this.Column.Name); var callExpression = Expression.Call(typeof(Enumerable), “Contains”, new Type[] { me.Type }, ce, me); 想法是用户从DropDownList中选择适当的Route然后我会检查Segment的RouteIds属性是否包含该Route的Id。 有关如何使其工作的任何指示? 编辑 – 以下是例外情况: 类型’System.Linq.Enumerable’上没有generics方法’Contains’与提供的类型参数和参数兼容。 如果方法是非generics的,则不应提供类型参数。

使用FieldInfo.SetValue与LINQ表达式在结构中设置字段

我想使用LINQ表达式设置私有字段。 我有这个代码: //parameter “target”, the object on which to set the field `field` ParameterExpression targetExp = Expression.Parameter(typeof(object), “target”); //parameter “value” the value to be set in the `field` on “target” ParameterExpression valueExp = Expression.Parameter(typeof(object), “value”); //cast the target from object to its correct type Expression castTartgetExp = Expression.Convert(targetExp, type); //cast the value to its correct […]

如何使用reflection找到特定的generics过载?

我正在尝试创建一个将调用特定generics重载方法的Expression (在我的第一个测试用例中为Enumerable.Average )。 特定类型绑定直到运行时才知道,因此我需要使用Reflection来查找和创建正确的generics方法( Expression是从解析后的文本创建的)。 所以,如果我在运行时知道我想找到这个特定的重载: public static double Average(this IEnumerable source, Func selector) 如何使用reflection解析特定的MethodInfo ? 到目前为止,我有以下选择声明: MethodInfo GetMethod(Type argType, Type returnType) { var methods = from method in typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static) where method.Name == “Average” && method.ContainsGenericParameters && method.GetParameters().Length == 2 && // and some condition where method.GetParameters()[1] is a Func that returns type […]

Linq表达式如何确定平等?

我正在考虑使用Linq Expression作为字典中的键。 但是,我担心我会得到奇怪的结果,因为我不知道Linq表达式如何确定Equality。 派生自Expression的类是否比较值相等或引用相等? 或者换句话说, Expression<Func> first = () => new object(); Expression<Func> second = ()=>new object(); bool AreTheyEqual = first == second;

具有null属性的嵌套属性上的动态linq顺序

我正在使用这个动态的linq orderby函数,我从这里得到了 。 这适用于嵌套属性,所以我可以这样做: var result = data.OrderBy(“SomeProperty.NestedProperty”); 问题是,如果SomeProperty为null,则在NestedProperty上执行OrderBy会抛出臭名昭着的“对象引用未设置为对象的实例”。 我的猜测是我需要自定义以下行来处理exception: expr = Expression.Property(expr, pi); // Or LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); 我考虑创建一个语句体,在最坏的情况下,我可以使用try catch,但这不起作用,因为你不能在orderby linq语句中有语句体:“带有语句体的lambda表达式无法转换为表达树“ 我迷失在这里,有关如何实现这一目标的任何建议? 顺便说一句,这是针对Linq to Objects,而不是数据库相关的。

如何将Func 隐式转换为Expression <Func >?

我不明白这里发生了什么: 这两行都编译: Func func = () => new object(); Expression<Func> expression = ()=>new object(); 但这不是: expression = func; LambdaExpression或Expression没有将委托转换为Expression的隐式运算符,因此必须进行其他操作才能使赋值工作。 它是什么?

使用LINQ进行动态表达。 如何找到厨房?

我尝试实现用户动态filter,其中使用选择一些属性,选择一些运算符并选择值。 由于我没有找到这个问题的答案,我尝试使用LINQ表达式。 主要是我需要确定主要房间是厨房的所有房屋(任何感觉,我知道)。 using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; //using System.Linq.Dynamic; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Room aRoom = new Room() { Name = “a Room” }; Room bRoom = new Room() { Name = “b Room” }; Room cRoom = new Room() { Name = “c […]

在Linq中使用Expression <Func >包含扩展名

使用以下示例我想在我的Contains方法中使用我的Expression ,让它使用EF将查询传递到sql server。 我怎样才能使这个正常工作? void Main() { IQueryable qry = GetQueryableItemsFromDB(); var filtered = qry.Filter(p=>p.CompanyId); } public static class Ext { public static IQueryable Filter(this IQueryable items, Expression<Func> resolveCompanyIdExpression) { IEnumerable validComps = GetCompanyIdsFromDataBase(); var exp = Expression.Lambda<Func>( Expression.Call(typeof(Queryable),”Contains”, new[] { typeof(Company) }, Expression.Constant(validComps), resolveCompanyIdExpression.Body), resolveCompanyIdExpression.Parameters[0]); return items.Where(exp); } public static IQueryable Filter(this IQueryable items, […]

LINQ成员表达式获取列名

你好, 我正在使用LINQ和EF与C#4.0。 我已将基本的ELMAH表拖入EF(已构建并保存了很多次)。 一切都按照人们的预期运作。 但是试图过于雄心勃勃并且需要一点帮助 – 我试图从作为变量传入的表达式中获取Column名称。 我想要的是这个: 传入:x => x.ErrorId 得到:“ErrorId” public void GetColumnName(Expression<Func> property) { // The parameter passed in x=>x.Message // Message works fine (probably because its a simple string) using: string columnName = (property.Body as MemberExpression).Member.Name; // But if I attempt to use the Guid or the date field then it […]