linq-expressions

How to build an IEnumerable<int>.Contains() Expression?

风格不统一 提交于 2019-12-01 12:12:31
I'm currently working with ASP Dynamic Data for the first time and I'm trying to build a filter. Our users have a need to locate items in a list based upon whether or not the item is a child of a selected parent (our items can have more than one parent). The items in question are Segments and each Segment has a property called RouteIds, of type IEnumerable, which is a collection of all of the Segment's parent Ids. I've gotten to this point in overriding the GetQueryable method in my filter, but I keep getting exceptions thrown on the last line shown: ConstantExpression ce = Expression.Constant

How to build an IEnumerable<int>.Contains() Expression?

£可爱£侵袭症+ 提交于 2019-12-01 09:41:43
问题 I'm currently working with ASP Dynamic Data for the first time and I'm trying to build a filter. Our users have a need to locate items in a list based upon whether or not the item is a child of a selected parent (our items can have more than one parent). The items in question are Segments and each Segment has a property called RouteIds, of type IEnumerable, which is a collection of all of the Segment's parent Ids. I've gotten to this point in overriding the GetQueryable method in my filter,

How to I find specific generic overload using reflection?

試著忘記壹切 提交于 2019-12-01 06:32:01
I am attempting to create an Expression that will invoke a specific generic overloaded method ( Enumerable.Average in my first test case). The specific type bindings are not known until runtime however so I need to use Reflection to find and create the correct generic method (the Expression is being created from parsed text). So if I know at runtime that I want to find this specific overload: public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector) How do I resolve that particular MethodInfo using reflection? So far I have the following selection

Calling (params object[]) with Expression[]

一世执手 提交于 2019-12-01 04:26:57
I'm trying to call String.Format from with in a Linq.Expression tree. Here's a quick example: var format = Expression.Constant("({0}) {1}"); var company = Expression.Property(input, membernames.First()); var project = Expression.Property(input, membernames.Last()); var args = new Expression[] {format, company, project}; var invoke = Expression.Call(method,args); The issue however is that String.Format has the signature of: String.Format(string format, params object[] args) and I'm trying to pass in Expression[]. Now I could go through all the trouble of creating an array, populating it with

How to I find specific generic overload using reflection?

别说谁变了你拦得住时间么 提交于 2019-12-01 04:20:13
问题 I am attempting to create an Expression that will invoke a specific generic overloaded method ( Enumerable.Average in my first test case). The specific type bindings are not known until runtime however so I need to use Reflection to find and create the correct generic method (the Expression is being created from parsed text). So if I know at runtime that I want to find this specific overload: public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)

Calling (params object[]) with Expression[]

陌路散爱 提交于 2019-12-01 02:00:39
问题 I'm trying to call String.Format from with in a Linq.Expression tree. Here's a quick example: var format = Expression.Constant("({0}) {1}"); var company = Expression.Property(input, membernames.First()); var project = Expression.Property(input, membernames.Last()); var args = new Expression[] {format, company, project}; var invoke = Expression.Call(method,args); The issue however is that String.Format has the signature of: String.Format(string format, params object[] args) and I'm trying to

How do Linq Expressions determine equality?

左心房为你撑大大i 提交于 2019-11-30 23:02:38
问题 I am considering using a Linq Expression as a key in a dictionary. However, I am concerned that I will get strange results, because I don't know how Equality is determined by Linq expressions. Does a class derived from Expression compare value equality or reference equality? Or in other words, Expression<Func<object>> first = () => new object(); Expression<Func<object>> second = ()=>new object(); bool AreTheyEqual = first == second; 回答1: Your test compares expressions . Expressions themselves

How is a Func<T> implicitly converted to Expression<Func<T>>?

筅森魡賤 提交于 2019-11-30 22:03:35
问题 I don't understand what is happening here: Both of these lines compile: Func<object> func = () => new object(); Expression<Func<object>> expression = ()=>new object(); But this doesn't: expression = func; There isn't an implicit operator on LambdaExpression or Expression<TDelegate> that converts a delegate to the expression, so something else must be happening to make the assignment work. What is it? 回答1: It's not an implicit conversion in the usual sense - it's a compiler trick. The compiler

Dynamic Expression using LINQ. How To Find the Kitchens?

荒凉一梦 提交于 2019-11-30 18:44:40
I try do implement a user dynamic filter, where used selects some properties, selects some operators and selects also the values. As I didn't find yet an answer to this question , I tried to use LINQ expressions. Mainly I need to identify all houses which main rooms are kitchens(any sens, I know). 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" };

LINQ member expression getting column name

谁说我不能喝 提交于 2019-11-30 07:39:47
Hello, I am using LINQ and EF with C# 4.0. I have dragged the basic ELMAH table into EF (built and saved many many times). All is working as one would expect. But have tried to be too ambitious and need a little help - I am trying to get the Column name from an expression that is passed in as a variable. What I want is this: Pass in : x=>x.ErrorId and get : "ErrorId" public void GetColumnName(Expression<Func<T, object>> property) { // The parameter passed in x=>x.Message // Message works fine (probably because its a simple string) using: string columnName = (property.Body as MemberExpression)