expression-trees

Expression visitor only calling VisitParameter for some lambda expressions

久未见 提交于 2019-12-11 05:46:14
问题 I want to be able to used nested extension methods to do projection of entities in EF to corresponding view models. (see my previous question Projection of single entities in EF with extension methods for more details on what im doing). As per this question I built an attribute to replace an extension method in an expression tree with a lambda to be able to do this. It takes the method arguments from the extentsion method and replaces them on as VisitParameter is called (I dont know if there

Using PredicateBuilder to build query searching across multiple columns of Entity

筅森魡賤 提交于 2019-12-11 05:39:43
问题 I have a list of field names. I am trying to build a predicate to look in the fields to see if they contain the search term. I have gone done the path listed in this original question but do not understand how to do a Contains instead of a NotEqual . string searchTerm = "Fred"; foreach (var field in FieldNames) { myPredicate= myPredicate.And(m => m.*field*.Contains(searchTerm)); } My code so far: public static Expression<Func<T, bool>> MultiColumnSearchExpression<T>(string fieldName,string

Can't build lambda Expression with unknown type of a property

旧街凉风 提交于 2019-12-11 05:37:49
问题 I am building a helper method that returns an expression based on a property that could be used in an orderby or where, etc. in a linq to entities operation. I won't know the type of the property upfront, so I have declared it as object and dynamic and have also tried to use Expression.Convert but it will not work for non string typed properties. The current property type that is not a string that I'm working with is int? and the error I get is Expression of type 'System.Nullable`1[System

Group by Dynamic Aggregate

守給你的承諾、 提交于 2019-12-11 04:33:56
问题 In the code below I want to replace x.BaseSalary with any other property whose name is stored in feildtoretrive : var feildtoretrive = "BaseSalary" var groupcal = db.Employees.GroupBy(x=>x.Region) .Select(group => new { Key = group.Key, Avg = group.Average(x => x.BaseSalary) }) .ToList(); 回答1: You need to create a MemberExpression that accesses that member of an instance compile a lambda expression that returns that member for a passed instance parameter (I assume that the elements in

Providing a generic key comparison based on a collection of a generic type

孤街浪徒 提交于 2019-12-11 04:28:45
问题 I have created my own InsertOrUpdate() implementations for a few types like this: public IEnumerable<Genre> InsertOrUpdate(IEnumerable<Genre> genres) { foreach (var genre in genres) { var existingGenre = _context.Genres.SingleOrDefault(x => x.TmdbId == genre.TmdbId); if (existingGenre != null) { existingGenre.Update(genre); yield return existingGenre; } else { _context.Genres.Add(genre); yield return genre; } } _context.SaveChanges(); } The return type of IEnumerable<T> is required because it

Polish/Prefix Notation logical expression to Expression Tree and back [closed]

天涯浪子 提交于 2019-12-11 03:23:14
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I am looking for a java library that could convert a logical expression wirtten in polish/prefix notation into an abstract syntax tree object and back. This is the syntax used by the SMT-LIB standard and CVC4 and I am trying to create some complex constraints. I don't want to evaluate the expression in any kind

Projection of single entities in EF with extension methods

怎甘沉沦 提交于 2019-12-11 01:54:22
问题 I like to do projection from my entity models into my view models using extension methods. This means I dont over/under fetch for my models and it makes the code nice and readable. It makes sense that sometimes the projections may include nested models, and I want to get reuse on those sub-projections. I want to be able to do something like the following: ctx.People.FiltersAndThings().ToViewModels();//the project my DB Models into view models Extension methods for actual projection public

Lambda compilation throws “variable '' of type '' referenced from scope '', but it is not defined”

断了今生、忘了曾经 提交于 2019-12-11 01:17:41
问题 When I attempt to compile the lambda shown below, it throws: variable 'model' of type 'System.Collections.Generic.IEnumerable`1[WheelEndCatalogKendo.Models.SapBasicData]' referenced from scope '', but it is not defined public static GridBoundColumnBuilder<TModel> BuildColumnString<TModel>(this GridBoundColumnBuilder<TModel> column, WebViewPage<IEnumerable<TModel>> webViewPage, int width) where TModel : class { var modelParameter = Expression.Parameter(typeof(IEnumerable<TModel>), "model");

Access children(list) related property in Expression tree

孤街醉人 提交于 2019-12-10 23:21:36
问题 I created a repository for my entity Master . In the repository, I have a Get method to get my entity by Id using Entity Core. The method receives: public TEntity Get(object id, params Expression<Func<TEntity, object>>[] includedRelatedEntities) { return GetById(IncludeEntities(DBContext.Set<TEntity>().AsQueryable(), includedRelatedEntities), id); } Then, when I use it in my code, I just pass to the method the id of the entity I´m looking for and and expression tree of the related entities

ExpressionTree rewrite - The parameter 'x' is not in scope

◇◆丶佛笑我妖孽 提交于 2019-12-10 22:54:09
问题 If I make any mistakes/mistypes in the following code, please don't get irrate, just drop a comment on here and I'll fix immediately - thanks Goal Re-map Expression<TDelegate> from one EntityA to a EntityB . I suspect this kind of thing has been done before but I haven't found any particularly useful links so feel free to point me in the right direction. What I have so far is a selection of classes that combine to allow for mappings to be created between Entity Members on two given classes.