linq-expressions

How do I access a Dictionary Item using Linq Expressions

我是研究僧i 提交于 2019-12-06 21:30:01
问题 I want to build a Lambda Expression using Linq Expressions that is able to access an item in a 'property bag' style Dictionary using a String index. I am using .Net 4. static void TestDictionaryAccess() { ParameterExpression valueBag = Expression.Parameter(typeof(Dictionary<string, object>), "valueBag"); ParameterExpression key = Expression.Parameter(typeof(string), "key"); ParameterExpression result = Expression.Parameter(typeof(object), "result"); BlockExpression block = Expression.Block(

Store Static Filter By Key Expression

自作多情 提交于 2019-12-06 10:12:18
I've got an function which generates an expression to filter a table by it's primary key, when passed in an Object[] , this is very similar to Find function except that it doesn't materialize so you can pass an IQueryable around afterwards public static Expression<Func<T, bool>> FilterByPrimaryKeyPredicate<T>(this DbContext dbContext, object[] id) { var keyProperties = dbContext.GetPrimaryKeyProperties<T>(); var parameter = Expression.Parameter(typeof(T), "e"); var body = keyProperties // e => e.{propertyName} == new {id = id[i]}.id .Select((p, i) => Expression.Equal( Expression.Property

Build a specific LINQ expression based on another LINQ expression and a value

前提是你 提交于 2019-12-06 09:43:01
If I've got a LINQ expression of the form: Expression<Func<MyClass, string, bool>> filterExpression = (x, filterVal) => x.DisplayName.Contains(filterVal); Is there any way I can get to the expression below? Expression<Func<MyClass, bool>> filter = x => x.DisplayName.Contains("John"); I need to use the second expression in a Linq-to-Entities Where call. You need to write an ExpressionVisitor that replaces the ParameterExpression with a ConstantExpression . It would look something like protected override Expression VisitParameter(ParameterExpression node) { if (node.Name == "filterVal") return

Invoke an Expression in a Select statement - LINQ to Entity Framework

纵然是瞬间 提交于 2019-12-06 06:54:26
问题 I'm trying to use an already existing Expression building class that I made when trying to do a select clause, but I'm not sure how to attach the expression to the expression tree for the Select, I tried doing the following: var catalogs = matchingCatalogs.Select(c => new { c.CatalogID, Name = EntitiesExpressionHelper.MakeTranslationExpression<Catalog>("Name", ApplicationContext.Instance.CurrentLanguageID).Compile().Invoke(c), CategoryName = EntitiesExpressionHelper.MakeTranslationExpression

C# Expression to sort generic query by key field

ε祈祈猫儿з 提交于 2019-12-06 06:22:24
问题 I have a generic method in which I want to sort an IQueryable<T> by its key field (it is safe to assume there is only one). Thus: void DoStuff<T>(...) { IQueryable<T> queryable = ... // given PropertyInfo keyField = ... // given var sortedQueryable = queryable.OrderBy(<some expression here>); ... } How do I define an Expression that will return the keyField property of T so that this will work? 回答1: This isn't too difficult, but you need to invoke the OrderBy with reflection as you don't know

Selecting Columns in LINQ using System.Linq.Expressions API

有些话、适合烂在心里 提交于 2019-12-06 02:04:49
I'm trying to use LINQ expressions to dynamically select columns from an IEnumerable into a result set that I can bind to my UI. At this point I am having a hard time just grasping the basics of projection in LINQ expressions. Let's say I have a list of strings like so: Dim myStrings = {"one", "two", "three"}.ToList() Using lambda expressions I can easily select out a collection of string lengths by doing: Dim myStringLengths = myStrings.Select(Function(x) x.Length) The result of this statement would leave me with a collection called myStringLengths that have the elements 3, 3, 5 . What I can

Dynamic Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> Expression

随声附和 提交于 2019-12-06 02:02:33
问题 I am using patterns mentioned here http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application And i am using method below to query EF public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = dbSet; if (filter != null) { query = query.Where

Building a custom predicate to act as a filter using a foreach loop

微笑、不失礼 提交于 2019-12-05 13:10:18
I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop : var mainPredicate = PredicateBuilder.True<Document>(); // mainPredicate is combined to other filters successfully here ... var innerPredicate = PredicateBuilder.False<Document>(); foreach (var period in periods) { var p = period; Expression<Func<Document, bool>> inPeriod = d => d.Date >= p.DateFrom && d.Date <= p.DateTo; innerPredicate = innerPredicate.Or(d => inPeriod.Invoke(d)); } mainPredicate = mainPredicate.And(innerPredicate); This last line : documents

Member Expression cannot convert to object from nullable decimal

别来无恙 提交于 2019-12-05 12:50:57
I am working on an MVC project and would like to pass the Html.TextboxFor method the name of a property. This is my viewmodel public class RuleViewModel<T> where T : class, IValidatableObject { private T _rule; public T RuleModel { get { return _rule ?? (_rule = Activator.CreateInstance<T>()); } } public RuleMetadata Metadata { get; set; } public Expression<Func<RuleViewModel<T>, Object>> GetParameterByName(PropertyInfo pi) { var fieldName = Expression.Parameter(typeof(RuleViewModel<T>), "x"); var fieldExpression = Expression.PropertyOrField(Expression.Property(fieldName, "RuleModel"), pi.Name

LINQ Expression for Contains

耗尽温柔 提交于 2019-12-05 09:32:53
I want to add dynamic expression in linq but facing issues on contains method it is working perfectly for Equal method Problem is i'm getting FilterField dynamically how to replace in query So far i had tried List<int> Ids = new List<int>(); **string filterField ="DEPARTMENT"; ==> Dynamic Field** var eParam = Expression.Parameter(typeof(EmployeeDetail), "e"); var comparison = Expression.Equal(Expression.Property(eParam, filterField), Expression.Convert(Expression.Constant(Ids), Expression.Property(eParam, filterField).Type)); var lambda = Expression.Lambda<Func<EmployeeDetail, bool>>