linq-expressions

LINQ member expression getting column name

送分小仙女□ 提交于 2019-11-29 09:41:09
问题 Hello, I am using LINQ and EF with C# 4.0. I have dragged the basic ELMAH table into EF (built and saved many many times). All is working as one would expect. But have tried to be too ambitious and need a little help - I am trying to get the Column name from an expression that is passed in as a variable. What I want is this: Pass in : x=>x.ErrorId and get : "ErrorId" public void GetColumnName(Expression<Func<T, object>> property) { // The parameter passed in x=>x.Message // Message works fine

Dynamic linq order by on nested property with null properties

本秂侑毒 提交于 2019-11-29 09:31:44
I'm using this dynamic linq orderby function which I got from here . This works fine with nested properties so I could do this: var result = data.OrderBy("SomeProperty.NestedProperty"); The problem is that if SomeProperty is null then performing the OrderBy on the NestedProperty throws the infamous "Object reference not set to an instance of an object". My guess is that I need to customize the following lines to handle the exception: expr = Expression.Property(expr, pi); // Or LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); I thought about creating a statement body where

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

纵然是瞬间 提交于 2019-11-29 00:02:45
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 = GetCompanyIdsFromDataBase(); var exp = Expression.Lambda<Func<T, bool>>( Expression.Call(typeof(Queryable),"Contains", new[] {

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

烈酒焚心 提交于 2019-11-28 19:38:57
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.Value == a + b); //2 + 3 Debug.Assert(vc.Value == 5); Debug.Assert(vc.IsValid == true); a = 7; Debug

Dynamic LINQ - Is There A .NET 4 Version?

左心房为你撑大大i 提交于 2019-11-28 17:01:56
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=="Pittsburgh"); or personData.Where(p => p.State=="PA"); I came across a blog post by Scott Guthrie

How to seed data with AddOrUpdate with a complex key in EF 4.3

随声附和 提交于 2019-11-28 15:50:54
I am trying to seed a development database with some test data. I have used context.People.AddOrUpdate(p => p.Id, people)); with much success. I have another table that I need to seed, in which I would not know the primary key. For example, I would want to AddOrUpdate based on the First and Last names matching. I am unsure how to write the Expression correctly. context.People.AddOrUpdate(p => p.FirstName && p.LastName, people); is obviously incorrect, but I hope it conveys the solution I am looking for. Try this: context.People.AddOrUpdate(p => new { p.FirstName, p.LastName }, people); If you

ExpressionVisitor soft delete

一笑奈何 提交于 2019-11-28 11:46:50
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 is in the select. The idea was to implement this IsDeleted plugin using an ExpressionVisitor which

Combine several similar SELECT-expressions into a single expression

时光怂恿深爱的人放手 提交于 2019-11-28 08:37:54
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 { Location = x.Locality.Name }; Expression<Func<Agency, AgencyDTO>> selector4 = x => new AgencyDTO {

Dynamic linq order by on nested property with null properties

一世执手 提交于 2019-11-28 02:57:15
问题 I'm using this dynamic linq orderby function which I got from here. This works fine with nested properties so I could do this: var result = data.OrderBy("SomeProperty.NestedProperty"); The problem is that if SomeProperty is null then performing the OrderBy on the NestedProperty throws the infamous "Object reference not set to an instance of an object". My guess is that I need to customize the following lines to handle the exception: expr = Expression.Property(expr, pi); // Or LambdaExpression

Fun (?) with Linq Expressions in extension methods

泪湿孤枕 提交于 2019-11-27 14:31:26
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(expression, htmlHelper.ViewData); var propertyName = metaData.PropertyName; var propertyValue = htmlHelper