linq-expressions

LINQ: Build dynamic filter with sequence of ANDs

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-04 04:35:12
问题 I'm struggling to dynamically create a query like that: Dictionary<string, Guid> parms = new Dictionary<string, Guid>(); foreach (var kvp in parms) { var exp = ReportDefinitions.Where(x=> x.Discriminants.Any(y=> y.Key == kvp.Key && y.Value == kvp.Value) // && more conditions to add here at each cycle ); } Where ReportDefinitions.Discriminants is an IDictionary<string, Guid> ; I know how to build simple Expression but I can't figure out how to build this one the "Any" seems really complicated.

Linq.Expression GetValue in VB?

筅森魡賤 提交于 2020-01-03 16:39:00
问题 Question: I have this C# program, that gets the value of field tablename of mytable. And it works fine. using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace AttachObjectsCS { static class Program { public class cmytable { public string tablename = "test"; } // http://stackoverflow.com/questions/2616638/access-the-value-of-a-member-expression private static object GetValue(System.Linq.Expressions.MemberExpression member) { System.Linq

Linq.Expression GetValue in VB?

可紊 提交于 2020-01-03 16:38:21
问题 Question: I have this C# program, that gets the value of field tablename of mytable. And it works fine. using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace AttachObjectsCS { static class Program { public class cmytable { public string tablename = "test"; } // http://stackoverflow.com/questions/2616638/access-the-value-of-a-member-expression private static object GetValue(System.Linq.Expressions.MemberExpression member) { System.Linq

.Net Core Localization View: IViewLocalizer inside Linq expression

我的梦境 提交于 2020-01-03 15:34:18
问题 i'm writing mvc app in .net core, i have problem with localization, i don't know how to add IViewLocalizer to my grid view. Here is my code: @using NonFactors.Mvc.Grid; @using Microsoft.AspNetCore.Mvc.Localization @inject IViewLocalizer Localizer @model IEnumerable<WeegreeEmployeeFormsCore.Models.Employee> @(Html .Grid(Model) .Build(columns => { columns.Add(model => model.Name).Titled(Localizer["Name"]).Sortable(true).Filterable(true); columns.Add(model => model.Surname).Titled(Localizer[

Expression tree for String.IndexOf method

旧巷老猫 提交于 2020-01-02 02:28:08
问题 How should I construct Expression tree for string.IndexOf("substring", StringComparison.OrdinalIgnoreCase) ? I can get it working without the second argument: StringComparison.OrdinalIgnoreCase . These are my attempts so far: var methodCall = typeof (string).GetMethod("IndexOf", new[] {typeof (string)}); Expression[] parms = new Expression[]{right, Expression.Constant("StringComparison.OrdinalIgnoreCase", typeof (Enum))}; var exp = Expression.Call(left, methodCall, parms); return exp; Also

Is reflection used when retrieving information from a linq expression?

丶灬走出姿态 提交于 2020-01-01 19:23:11
问题 I have the following extension method: public static string ToPropertyName<T,E>(this Expression<Func<E, T>> propertyExpression) { if (propertyExpression == null) return null; string propName; MemberExpression propRef = (propertyExpression.Body as MemberExpression); UnaryExpression propVal = null; // -- handle ref types if (propRef != null) propName = propRef.Member.Name; else { // -- handle value types propVal = propertyExpression.Body as UnaryExpression; if (propVal == null) throw new

use Expression<Func<T,X>> in Linq contains extension

只谈情不闲聊 提交于 2019-12-29 04:50:39
问题 Using the following example i would like to use my Expression inside my Contains method, having it pass the query onto sql server using the EF . How can i build this up to work correctly? void Main() { IQueryable<Person> qry = GetQueryableItemsFromDB(); var filtered = qry.Filter(p=>p.CompanyId); } public static class Ext { public static IQueryable<T> Filter<T>(this IQueryable<T> items, Expression<Func<T, int>> resolveCompanyIdExpression) { IEnumerable<int> validComps =

Can't use ternary operator to assign Linq expression

纵饮孤独 提交于 2019-12-29 01:15:26
问题 I just typed the following code: Expression<Func<ContentItem, bool>> expression = fileTypeGroupID.HasValue ? n => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value : n => true; Visual Studio is saying it can't infer the type of n . The code seems fine to me - it's just using a ternary operator to assign one of two Expression literals to an Expression variable. Is Visual Studio just not smart enough to infer the type of n inside a ternary operator, or have I made some kind of

Handle null ref exceptions in LINQ Expression calls

◇◆丶佛笑我妖孽 提交于 2019-12-25 03:13:34
问题 I am trying to create a generic Expression builder, which basically works fine as long as none the objects values is null. My current code looks like this (StartsWith as an example): case FilterOperationTypes.StartsWith: { ParameterExpression e = Expression.Parameter(typeof(T), "e"); PropertyInfo propertyInfo = typeof(T).GetProperty(field); MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo); ConstantExpression c = Expression.Constant(val, typeof(string)); MethodInfo mi = typeof

Invoking lambda expressions in Expression trees

流过昼夜 提交于 2019-12-25 02:58:18
问题 I have a SelectionCriteria class that I use for building Entity Framework query expressions, based on PredicateBuilder. Within its limits, it's working fine. I'd like to extend it so that it can query whether a field contains a substring. My problem is that I can't see how to build the needed expression object. My actual class supports and, or, and not, but they aren't relevant to my question. So I've simplified my example code to handle only a single binary operation: public class