linq-expressions

What is the best way to merge two objects during runtime using C#?

大憨熊 提交于 2019-12-03 13:23:17
问题 I have two objects and I want to merge them: public class Foo { public string Name { get; set; } } public class Bar { public Guid Id { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public string Property3 { get; set; } public string Property4 { get; set; } } To create: public class FooBar { public string Name { get; set; } public Guid Id { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public string Property3 {

Converting a lambda expression into a unique key for caching

僤鯓⒐⒋嵵緔 提交于 2019-12-03 08:26:24
问题 I've had a look at other questions similar to this one but I couldn't find any workable answers. I've been using the following code to generate unique keys for storing the results of my linq queries to the cache. string key = ((LambdaExpression)expression).Body.ToString(); foreach (ParameterExpression param in expression.Parameters) { string name = param.Name; string typeName = param.Type.Name; key = key.Replace(name + ".", typeName + "."); } return key; It seems to work fine for simple

How to convert a LambdaExpression to typed Expression<Func<T, T>>

只愿长相守 提交于 2019-12-03 05:05:17
I'm dynamically building linq queries for nHibernate. Due to dependencies, I wanted to cast/retrieve the typed expression at a later time, but I have been unsuccessfull so far. This is not working (the cast is supposed to happen elsewhere): var funcType = typeof (Func<,>).MakeGenericType(entityType, typeof (bool)); var typedExpression = (Func<T, bool>)Expression.Lambda(funcType, itemPredicate, parameter); //Fails This is working: var typedExpression = Expression.Lambda<Func<T, bool>>(itemPredicate, parameter); Is it possible to get the 'encapsulated' typed expression from a LambdaExpression?

Converting a lambda expression into a unique key for caching

自古美人都是妖i 提交于 2019-12-02 21:04:13
I've had a look at other questions similar to this one but I couldn't find any workable answers. I've been using the following code to generate unique keys for storing the results of my linq queries to the cache. string key = ((LambdaExpression)expression).Body.ToString(); foreach (ParameterExpression param in expression.Parameters) { string name = param.Name; string typeName = param.Type.Name; key = key.Replace(name + ".", typeName + "."); } return key; It seems to work fine for simple queries containing integers or booleans but when my query contains nested constant expressions e.g. // Get

Dynamically calling methods corresponding a parameter's type using expression trees in c#

空扰寡人 提交于 2019-12-02 11:24:13
问题 I'm building an event handler which will work similarly to how aggregates behave in event sourced systems. What I'm trying to achieve can be done in ways as documented here Other references I've looked into are the Marten source code and Greg Young's m-r. I want to achieve the same with Expression Trees. In essence, I want my aggregate implementation to dynamically execute events passed to it if it has a Handle method which accepts that event as a parameter. First I have my events abstract

Entity Navigation Property IQueryable cannot be translated into a store expression

这一生的挚爱 提交于 2019-12-02 11:08:50
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 }).ToList() }); return departments.ToList(); } by using this function: public static IQueryable

How do I reuse an Expression on a single object in another Expression

余生长醉 提交于 2019-12-02 09:56:09
问题 I feel like I am missing something simple, but I have not found the documentation that answers my question. I have recently been decomposing some of the linq projections into reusable expressions. It works great when operating on a collection, but I can't seem to figure out how to apply an expression to a single object in another expression. Below is an example of what I am trying to accomplish: public class Person { public string ID { get; set; } public string Name { get; set; } } public

Composing invocations with Expression<Func<T,bool>> the same way as Func<T,bool>

不羁的心 提交于 2019-12-02 08:06:25
Consider a class that can be used as a member of multiple other classes: class Customer { public string FirstName {get;set;} public string LastName {get;set;} } // Both "Order" and "Profile" have a "Customer" property class Order { public Customer Customer {get;set;} } class Profile { public Customer Customer {get;set;} } I want to define a method that makes a checker for an object associated with a Customer . If I want an in-memory checker, I do it like this: static Func<T,bool> Check<T>(Func<T,Customer> conv, string first, string last) { return obj => conv(obj).FirstName == first && conv(obj

Expression Tree for o?.Value

老子叫甜甜 提交于 2019-12-02 05:33:42
问题 I'd like to generate this sentence using Expression trees: o?.Value o is an instance of whichever class. Is there some way? 回答1: Normally, if you want to know how to construct an expression tree for some expression, you let the C# compiler do it and inspect the result. But in this case, it won't work, because "An expression tree lambda may not contain a null propagating operator." But you don't actually need the null propagating operator, you just need something that behaves like one. You can

Dynamically calling methods corresponding a parameter's type using expression trees in c#

試著忘記壹切 提交于 2019-12-02 04:44:39
I'm building an event handler which will work similarly to how aggregates behave in event sourced systems. What I'm trying to achieve can be done in ways as documented here Other references I've looked into are the Marten source code and Greg Young's m-r. I want to achieve the same with Expression Trees. In essence, I want my aggregate implementation to dynamically execute events passed to it if it has a Handle method which accepts that event as a parameter. First I have my events abstract class Event { } class Event1 : Event { } class Event2 : Event { } I have my aggregate implementation