linq-expressions

Linq Join with Dynamic Expression

会有一股神秘感。 提交于 2019-12-24 13:19:58
问题 I'm attempting to do a dynamic join in linq. Meaning that I only know at runtime what field the join will occur on. I've done the following: var itemParam = Expression.Parameter(typeof(E), "obj"); var entityAccess = Expression.MakeMemberAccess(Expression.Parameter(typeof(E), "obj"), typeof(E).GetMember(Field).First()); var lambda = Expression.Lambda(entityAccess, itemParam); var q = dbSet.Join(context.Acl, lambda, acl => acl.ObjectID, (entity, acl) => new { Entity = entity, ACL = acl });

C# Expression to use Guid bookmark

别等时光非礼了梦想. 提交于 2019-12-24 03:35:16
问题 I have a requirement to work through several tables that need to be synchronized/backed up. All of these tables' classes implement ITrackModifiedDate : interface ITrackModifiedDate { DateTime ModifiedDate { get; set; } } I need to work through these in batches, which means I need to bookmark where I last got up to. So I can sort by ModifiedDate , and just keep track of my last ModifiedDate . But there's a wrinkle in the plan: it can easily happen that multiple records have the identical

Is there a way to set 'DeclaringType' in an expression tree?

我与影子孤独终老i 提交于 2019-12-23 09:19:30
问题 I am doing a Func -> Expression -> Func conversion. It works fine if I create the Func<>() from a method(first example below) however if I create the function using an expression tree(2nd example) it fails with a NullReferenceException when accessing func2.Method.DeclaringType.FullName . And this is because DeclaringType is null. (NJection uses reflection so I think that is why it needs DeclaringType.) How can I fill in DeclaringType type for the Func<> that was created by compiling an

Store Static Filter By Key Expression

那年仲夏 提交于 2019-12-22 16:30:54
问题 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 =

How to compare only date part with linq expression?

荒凉一梦 提交于 2019-12-21 16:56:36
问题 I just want to make Column Filter for grid view. Simple I just want to filter grid view column with some extra stuff. Here I have created one IQueryable that returns queryable result. Here is my code : ------------------------------------------Updated------------------------------------------------ public static IQueryable<T> FilterForColumn<T>(this IQueryable<T> queryable, string colName, string searchText) { if (colName != null && searchText != null) { var parameter = Expression.Parameter

What is the purpose of LINQ's Expression.Quote method?

点点圈 提交于 2019-12-20 09:14:58
问题 The MSDN documentation states: Expression.Quote Method Creates a UnaryExpression that represents an expression that has a constant value of type Expression. I've been able to build predicate expressions for use in LINQ queries by manually constructing them using the Expression class, but have never come across the need for Expression.Quote. When and why would you use this? From the LINQ expressions I've seen that have them, they just seem to wrap existing expressions without adding any value.

What is the purpose of LINQ's Expression.Quote method?

限于喜欢 提交于 2019-12-20 09:13:36
问题 The MSDN documentation states: Expression.Quote Method Creates a UnaryExpression that represents an expression that has a constant value of type Expression. I've been able to build predicate expressions for use in LINQ queries by manually constructing them using the Expression class, but have never come across the need for Expression.Quote. When and why would you use this? From the LINQ expressions I've seen that have them, they just seem to wrap existing expressions without adding any value.

Entity Navigation Property IQueryable cannot be translated into a store expression

我只是一个虾纸丫 提交于 2019-12-20 06:00:24
问题 im using Entity Framework designer first and I need to create custom Model Objects starting from the db objects. I don't want to use IEnumerable cause it will query too many fields. The goal is to remove the inner select within this function: using (var db = new dbEntities()) { var departments= db.departments .Include(p => p.employee) .Where(...) .Select(p => new CustomDepartmentModel() { ID = p.ID, Employees = p.employee .Select(q => new CustomEmployeeModel() { ID = q.ID, Name= q.Name })

Dynamic Expression using LINQ. How To Find the Kitchens?

谁都会走 提交于 2019-12-18 22:16:14
问题 I try do implement a user dynamic filter, where used selects some properties, selects some operators and selects also the values. As I didn't find yet an answer to this question, I tried to use LINQ expressions. Mainly I need to identify all houses which main rooms are kitchens(any sens, I know). using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; //using System.Linq.Dynamic; namespace ConsoleApplication2 { class Program { static void Main(string[

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

点点圈 提交于 2019-12-17 21:42:21
问题 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