linq-expressions

How do I compose Linq Expressions? ie Func<Exp<Func<X, Y>>, Exp<Func<Y, Z>>, Exp<Func<X, Z>>>

十年热恋 提交于 2019-11-27 12:26:28
问题 I'm creating a Validator<T> class. I'm attempting to implement the Linq SelectMany extension methods for my validator to be able to compose expressions using a Linq query and validate the final result even when the underlying values change. The following test code demonstrates my intent. var a = 2; var b = 3; var va = Validator.Create(() => a, n => n >= 0 && n < 5); var vb = Validator.Create(() => b, n => n >= 0 && n < 5); var vc = from ia in va from ib in vb select ia + ib; Debug.Assert(vc

How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>?

删除回忆录丶 提交于 2019-11-27 11:27:16
I trying to append where predicates and my goal is to create the same expression as: Services.Where(s => s.Name == "Modules" && s.Namespace == "Namespace"); I have the following code: Expression<Func<Service,string>> sel1 = s => s.Name; Expression<Func<Service,string>> sel2 = s => s.Namespace; var val1 = Expression.Constant("Modules"); var val2 = Expression.Constant("Namespace"); Expression e1 = Expression.Equal(sel1.Body, val1); Expression e2 = Expression.Equal(sel2.Body, val2); var andExp = Expression.AndAlso(e1, e2); ParameterExpression argParam = Expression.Parameter(typeof(string), "s");

Dynamic LINQ - Is There A .NET 4 Version?

人盡茶涼 提交于 2019-11-27 10:21:09
问题 I'm looking to use LINQ for some searching routines and wanted to have some dynamic where clauses. So, for example, if a user wants to search by city or search by state, I would have a dynamic LINQ Where<> call instead of creating two strongly typed LINQ expressions and then using the appropriate one based on how the user wants to search. So I would like to do this: String criteria="p.City='Pittsburgh'"; //or "p.State='PA'" personData.Where(criteria) instead of personData.Where(p => p.City==

Why are some object properties UnaryExpression and others MemberExpression?

孤人 提交于 2019-11-27 07:43:44
Acting on the answer to my Select a model property using a lambda and not a string property name question, wanting to add properties to a collection as follows: var props = new ExportPropertyInfoCollection<JobCard>(); props.Include(model => model.BusinessInstallNumber).Title("Install No").Width(64).KeepZeroPadding(true); props.Include(model => model.DeviceName).Title("Device").Width(70); props.Include(model => model.DateRequested).Title("Request Date").Format("{0:dd/MM/yyyy}").Width(83); I wrote the following code in the Include method: public class PropertyCollection<T> { public void Include

ExpressionVisitor soft delete

╄→гoц情女王★ 提交于 2019-11-27 06:26:47
问题 We're having some issues implementing soft delete functionality with entity framework. The idea is to use a repository which is aware of the EF context. On the level of the repository we implemented a plugin system, these plugins get executed whenever an action is done on the repository. For example when we call Repository.GetQuery<Relation>() the plugins get executed. One of the plugins is a LogicalDeletePlugin , this plugin should add a Where(x => x.IsDeleted) statement to each table which

Combine several similar SELECT-expressions into a single expression

六眼飞鱼酱① 提交于 2019-11-27 02:21:00
问题 How to combine several similar SELECT-expressions into a single expression? private static Expression<Func<Agency, AgencyDTO>> CombineSelectors(params Expression<Func<Agency, AgencyDTO>>[] selectors) { // ??? return null; } private void Query() { Expression<Func<Agency, AgencyDTO>> selector1 = x => new AgencyDTO { Name = x.Name }; Expression<Func<Agency, AgencyDTO>> selector2 = x => new AgencyDTO { Phone = x.PhoneNumber }; Expression<Func<Agency, AgencyDTO>> selector3 = x => new AgencyDTO {

Fun (?) with Linq Expressions in extension methods

让人想犯罪 __ 提交于 2019-11-26 18:24:48
问题 I wrote an HtmlHelper expression I use a lot of the time to put title tags into my dropdown lists like so: public static HtmlString SelectFor<TModel, TProperty, TListItem>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TListItem> enumeratedItems, string idPropertyName, string displayPropertyName, string titlePropertyName, object htmlAttributes) where TModel : class { //initialize values var metaData = ModelMetadata.FromLambdaExpression

How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>?

雨燕双飞 提交于 2019-11-26 15:34:44
问题 I trying to append where predicates and my goal is to create the same expression as: Services.Where(s => s.Name == "Modules" && s.Namespace == "Namespace"); I have the following code: Expression<Func<Service,string>> sel1 = s => s.Name; Expression<Func<Service,string>> sel2 = s => s.Namespace; var val1 = Expression.Constant("Modules"); var val2 = Expression.Constant("Namespace"); Expression e1 = Expression.Equal(sel1.Body, val1); Expression e2 = Expression.Equal(sel2.Body, val2); var andExp =

Why are some object properties UnaryExpression and others MemberExpression?

我的未来我决定 提交于 2019-11-26 13:23:05
问题 Acting on the answer to my Select a model property using a lambda and not a string property name question, wanting to add properties to a collection as follows: var props = new ExportPropertyInfoCollection<JobCard>(); props.Include(model => model.BusinessInstallNumber).Title("Install No").Width(64).KeepZeroPadding(true); props.Include(model => model.DeviceName).Title("Device").Width(70); props.Include(model => model.DateRequested).Title("Request Date").Format("{0:dd/MM/yyyy}").Width(83); I

Convert Linq expression “obj => obj.Prop” into “parent => parent.obj.Prop”

混江龙づ霸主 提交于 2019-11-26 00:44:22
问题 I have an existing expression of type Expression<Func<T, object>> ; it contains values like cust => cust.Name . I also have a parent class with a field of type T . I need a method that accepts the above as a parameter and generates a new expression that takes the parent class ( TModel ) as a parameter. This will be used as an expression parameter of an MVC method. Thus, cust => cust.Name becomes parent => parent.Customer.Name . Likewise, cust => cust.Address.State becomes parent => parent