linq-expressions

Using Expression.Call with Queryable.Select with a type known only at runtime

本秂侑毒 提交于 2020-01-23 17:49:07
问题 I am trying to select a column from an IEnumerable collection that has a type known only to me at runtime. The only way I can think of using this is using LINQ expressions to build a dynamic call to Queryable.Select . However, I'm having a lot of trouble figuring out the proper syntax to accomplish this. An example of how I would do this in the happy-go-lucky world of knowing everything I need at compile time, my code would look like this: ' Create an IEnumerable(Of String) Dim strings = {

Reusable MVC code: handling value types in linq expressions

别说谁变了你拦得住时间么 提交于 2020-01-15 04:24:48
问题 Overview I am writing an MVC set that is intended to provide basic search/add/edit/delete functionality for an Entity Framework object. It also does some fancy things like supporting dropdowns filled from other tables using attributes. The main object in play is an inheritable generic controller ( AdminController<T> where T is the EF object) containing an array of AdminField<T> , each element of which represents a field/property in T . AdminController<T> can generate an AdminViewModel<T>

Dynamic Expression Generation Issues with ValueTypes

独自空忆成欢 提交于 2020-01-14 19:42:10
问题 I built a framework that allows for cascaded sorting of report data in a table depending on what column is the master sorted column. It works for the most part, except in one specific, but important case: when the field's property is a value type. I receive the following error message: System.ArgumentException: Expression of type 'System.Int32' cannot be used for return type 'System.Object' I know that this means I need to box the value of the ValueType, but I'm not completely sure how to in

Generic expression abstraction issue

隐身守侯 提交于 2020-01-13 06:09:11
问题 I have the following method SetMapping() which is used to define some mapping setting using expressions. public class AggregateMap<TDataEntity> { protected Expression<Func<IUpdateConfiguration<TDataEntity>, object>> graphMapping; protected void SetMapping(Expression<Func<IUpdateConfiguration<TDataEntity>, object>> mapping) { graphMapping = mapping; } } Example calling code: SetMapping(map => map.OwnedCollection(root => root.ChildEntities)); The above works great, but I would like to abstract

How to set a breakpoint in a lambda expression?

元气小坏坏 提交于 2020-01-12 08:20:25
问题 I would like to debug a lambda that is called in an expression tree. Unfortunately, the breakpoint is never hit. Here's a full Console Program to play with: private static void Main() { var evalAndWrite = EvalAndWrite(x => x + 1 /* a breakpoint here is never hit */); evalAndWrite(1); Console.ReadLine(); } private static Action<int> EvalAndWrite(Expression<Func<int, int>> expr) { var result = Expression.Variable(typeof(int), "result"); var assign = Expression.Assign(result, expr.Body); var

How to convert a LambdaExpression to typed Expression<Func<T, T>>

巧了我就是萌 提交于 2020-01-12 04:24:25
问题 I'm dynamically building linq queries for nHibernate. Due to dependencies, I wanted to cast/retrieve the typed expression at a later time, but I have been unsuccessfull so far. This is not working (the cast is supposed to happen elsewhere): var funcType = typeof (Func<,>).MakeGenericType(entityType, typeof (bool)); var typedExpression = (Func<T, bool>)Expression.Lambda(funcType, itemPredicate, parameter); //Fails This is working: var typedExpression = Expression.Lambda<Func<T, bool>>

Composing invocations with Expression<Func<T,bool>> the same way as Func<T,bool>

*爱你&永不变心* 提交于 2020-01-11 13:01:14
问题 Consider a class that can be used as a member of multiple other classes: 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;} } I want to define a method that makes a checker for an object associated with a Customer . If I want an in-memory checker, I do it like this: static Func<T,bool> Check<T>

Using FieldInfo.SetValue vs LINQ expressions to set a field in a struct

泪湿孤枕 提交于 2020-01-11 05:40:08
问题 I want to set private fields using LINQ expressions. I have this code: //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

LINQ to Entities does not recognize the method 'System.String ToBase64String(Byte[])' method,

家住魔仙堡 提交于 2020-01-07 09:28:11
问题 LINQ to Entities does not recognize the method 'System.String ToBase64String(Byte[])' method, and this method cannot be translated into a store expression. var activityList = (from item in committeeMemberList let committee = db.Committee.FirstOrDefault(x => x.Committee_Id == item.Committee_Id) let contact = db.Contacts.FirstOrDefault(x => x.Contact_Id == item.Contact_Id) select new Activity { Id = Convert.ToBase64String(item.Committee_Member_SPE_Id), Name = committee.Committee_Name, ... ... }

How to apply generic expression-trees code that works well on integers, to enums (e.g. how to make Expression.PostIncrementAssign() work on enums?)?

你离开我真会死。 提交于 2020-01-05 04:44:05
问题 I'm trying to create an extension method to do this: enum AlphaBet { A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z } IEnumerable<AlphaBet> rangeCtoG = AlphaBet.C.RangeToInc(AlphaBet.G); But this won't compile (as TEnum is generic): public static IEnumerable<TEnum> RangeToInc<TEnum>(this TEnum from, TEnum to) where TEnum : struct, IComparable, IFormattable, IConvertible // { for (; from <= to; from++) yield return from; } So I turned to expressions: public delegate void MyFunc<T>(ref T