expression-trees

Expression trees for dummies? [closed]

你离开我真会死。 提交于 2019-12-17 21:39:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I am the dummy in this scenario. I've tried to read on Google what these are but I just don't get it. Can someone give me a simple explanation of what they are and why they're useful? edit: I'm talking about the LINQ feature in .Net. 回答1: The best explanation about expression

Calling a Generic Method using Lambda Expressions (and a Type only known at runtime)

佐手、 提交于 2019-12-17 19:33:44
问题 You can use Lambda Expression Objects to represent a lambda as an expression. How do you create a Lambda Expression Object representing a generic method call, if you only know the type -that you use for the generic method signature- at runtime? For example: I want to create a Lambda Expression Objects to call: public static TSource Last<TSource>( this IEnumerable<TSource> source ) But I only know what TSource is at runtime. 回答1: static Expression<Func<IEnumerable<T>, T>> CreateLambda<T>() {

LinqKit System.InvalidCastException When Invoking method-provided expression on member property

一个人想着一个人 提交于 2019-12-17 18:17:53
问题 Given a simple parent/child class structure. I want to use linqkit to apply a child lambda expression on the parent. I also want the Lambda expression to be provided by a utility method. public class Foo { public Bar Bar { get; set; } } public class Bar { public string Value { get; set; } public static Expression<Func<Bar, bool>> GetLambdaX() { return c => c.Value == "A"; } } ... Expression<Func<Foo, bool>> lx = c => Bar.GetLambdaX().Invoke(c.Bar); Console.WriteLine(lx.Expand()); The above

Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'

这一生的挚爱 提交于 2019-12-17 17:57:13
问题 I created a method in C# to get methodname public string GetCorrectPropertyName<T>(Expression<Func<T, string>> expression) { return ((MemberExpression)expression.Body).Member.Name; // Failure Point } and calling it as string lcl_name = false; public string Name { get { return lcl_name ; } set { lcl_name = value; OnPropertyChanged(GetCorrectPropertyName<ThisClassName>(x => x.Name)); } } This works fine if property is string and for all other types gives this exception: Unable to cast object of

Reverse of Expression<Func<T,TResult>>.Compile()?

对着背影说爱祢 提交于 2019-12-17 16:58:31
问题 Since we can: Expression<Func<int, bool>> predicate = x => x > 5; var result = Enumerable.Range(0,10).Where(predicate.Compile()); How can I: Func<int,bool> predicate = x => x > 5; Expression<Func<int,bool>> exp = predicate.Decompile(); That is, I want to get the corresponding Expression of the Func . Is it possible? 回答1: There is no magic Decompile() for a delegate instance, short of deconstructing the IL (perhaps with mono.cecil). If you want an expression tree, you'll have to start with an

Access the value of a member expression

不问归期 提交于 2019-12-17 15:14:28
问题 If i have a product. var p = new Product { Price = 30 }; and i have the following linq query. var q = repo.Products().Where(x=>x.Price == p.Price).ToList() In an IQueryable provider, I get a MemberExpression back for the p.Price which contains a Constant Expression, however I can't seem to get the value "30" back from it. Update I have tried this but it doesn't seem to work. var memberExpression = (MemberExpression)GetRootConstantExpression(m); var fi = (PropertyInfo)memberExpression.Member;

Generic DbDataReader to List<T> mapping

女生的网名这么多〃 提交于 2019-12-17 07:41:33
问题 I am having a slight issue (more like an annoyance) with my property binding data access classes. The problem is that the mapping fails when there exists no column in the reader for corresponding property in class. Code Here is the mapper class: // Map our datareader object to a strongly typed list private static IList<T> Map<T>(DbDataReader dr) where T : new() { try { // initialize our returnable list List<T> list = new List<T>(); // fire up the lamda mapping var converter = new Converter<T>

Generic DbDataReader to List<T> mapping

不羁岁月 提交于 2019-12-17 07:41:15
问题 I am having a slight issue (more like an annoyance) with my property binding data access classes. The problem is that the mapping fails when there exists no column in the reader for corresponding property in class. Code Here is the mapper class: // Map our datareader object to a strongly typed list private static IList<T> Map<T>(DbDataReader dr) where T : new() { try { // initialize our returnable list List<T> list = new List<T>(); // fire up the lamda mapping var converter = new Converter<T>

Create Expression Tree For Selector

≡放荡痞女 提交于 2019-12-13 17:16:06
问题 Related To : Create a Lambda Expression With 3 conditions Convert Contains To Expression Tree Convert List.Contains to Expression Tree Please consider above questions. I want to write a query for this: using (MyEntities context = new MyEntities()) { var DbSet = context.CreateObjectSet<T>(); var Max = DbSet.Where(exp).Select(selector).Max(); } I don't know how to write a code for selector . What Select overload I should use? and How I can write that using Expression Tree? Thanks 回答1: What

OrderBy Expression Tree in Net Core Linq for Extension Method

主宰稳场 提交于 2019-12-13 03:26:45
问题 I want to create an Extension method which mimics this, https://dejanstojanovic.net/aspnet/2019/january/filtering-and-paging-in-aspnet-core-web-api/ However, I want to add an OrderBy (for ColumnName) after StartsWith, how would I conduct this? tried adding following and did not work .OrderBy(parameter) Example: return persons.Where(p => p.Name.StartsWith(filterModel.Term ?? String.Empty, StringComparison.InvariantCultureIgnoreCase)) .OrderBy(c=>c.Name) .Skip((filterModel.Page-1) * filter