zzz projects Expression Tree Tutorial
Getting Started Documentation Knowledge Base
  • Getting Started
  • Documentation
  • Knowledge Base

Expression Tree Tutorial - Knowledge Base (KB)

19 results in tag: generics

What is the best way to create a Linq Expression Tree that compares to a generic object?

I have an IQueryable and an object of type T....I want to do IQueryable().Where(o => o.GetProperty(fieldName) == objectOfTypeT.GetProperty(fieldName))...so ......public IQueryable<T> DoWork<T>(string fieldName) where T : EntityObject { ... T...
c# entity-framework expression-trees generics linq
asked by JTew

Refactoring this Generic GetByID function in C# LINQ to SQL

I wrote the following method....public T GetByID(int id) { var dbcontext = DB; var table = dbcontext.GetTable<T>(); return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) == id); } ...
c# expression-trees generics linq-to-sql
asked by Andreas Grech

Using Lambda Expressions to Call a Generic Method (and a Type only known at runtime)

You can use ...Lambda Expression Objects... to represent a lambda as an expression....How do you create a ...Lambda Expression Object... representing a generic method call, if you only know the type -that you use for the generic method signature- at runt...
c# expression expression-trees generics lambda
asked by SDReyes

Is there a general technique for iterating over and printing values from an unknown collection?

Let's say, I have a Print method like this:...private static void Print(IEnumerable items) { // Print logic here } ...I want to pass a collection class to this Print method, which should print all the fields like a table. For example, my input collect...
.net c# expression-trees generics lambda
asked by Prince Ashitaka

What is the difference between ExpressionFuncT, bool>> and ExpressionFuncType, bool>>?

I am trying build a dictionary of Expressions that have different input parameter types. I am trying to store the type of the parameter because later down the road I plan to use Reflection to discover a method on the type. Here is the code that creates th...
expression-trees functional-programming generics linq reflection
asked by Rockdocta

How can I determine the type of a property in a generic expression?

public static void Map<T>(Expression<Func<T, object >> expression) { var memberExpression = (expression.Body as UnaryExpression).Operand as MemberExpression; var name = memberExpression.Member.Name; } ...Usage:...Map<Article>(x => x.Name) ...Name....
c# expression expression-trees generics
asked by Rookian

How to Make an Expression When a type isn't known until runtime, should you use lambda?

This is best explained using code. I have a generic class that has a method that returns an integer. Here is a simple version for the purposes of explaining......public class Gen<T> { public int DoSomething(T instance) { // Real code does ...
c# expression-trees generics lambda
asked by Phil Wright

Create a string expression (null reference)

Here is the problem:...We use table object to allow the users to perform some functionality like search, sort, pagination etc. Those tables works great. But there is a problem with one of the functionality : The sort (=OrderBy)....In fact, to allow the so...
c# expression expression-trees generics sql-order-by
asked by Whoami

For delegate parameters of type 'System.String,' a parameterExpression of type 'System.Int32' cannot be used.

I'm creating a general setter using expression tree and here is my code...public Expression<Action<T,string>> GetAction<T>(string fieldName) { ParameterExpression targetExpr = Expression.Parameter(typeof(T), "Target"); MemberExpression fieldExpr...
c# delegates dynamic expression-trees generics
asked by kans

At runtime, create a generic type with a generic interface.

I've been working through an issue for a couple of hours now, and I think I'm close. I'm working on an app where we could have 50-100 types that perform the same way. So instead of creating 50-100 classes, I tried to make it generic and this is what I hav...
.net c# expression-trees generics linq
asked by Bob Horn

How can I simplify Linq extension method invokation using reflection?

Background:...I have a web service that returns the rows in a table (table name supplied as a parameter) with Ids greater than a certain Id (also supplied as a parameter). We are assuming the Ids are sequential....I am using Linq to SQL for database inter...
c# expression-trees generics linq reflection
asked by Ryan Kirkman

Generic expression for where clause - "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."

I am trying to write a really generic way to load EF entities in batches, using the Contains method to generate a SQL IN statement. I've got it working if I pass the entire expression in, but when I try to build the expression dynamically, I am getting a...
entity-framework expression-trees generics linq linq-to-sql
asked by Randar Puust

Expression Tree using Action<T> possible?

I am having a little trouble with something I am working on. I initially created a generic layer that sits between my business objects and the data access layer which is working fine. I then recently read about something called Expression Trees which is a...
c# expression-trees generics reflection
asked by Dr Schizo

Expression<Func<T, bool>> with It.IsAny always return true

I'm trying to create a generic testclass to test my generic controllers. Everything is working fine except this: I have a method like this: ...private T GetSingle(Expression<Func<T, bool>> expression) ...I'm trying to setup the test like so:...var Guids =...
c# expression-trees generics moq unit-testing
asked by Johan Nordin

Creating expression tree for accessing a Generic type's property

I need to write a generic method which takes the instance of the generic type and the property name in string format and return an Expression tree...I need to convert a simple lambda expression...a => a.SomePropertyName ...where ...a... is generic type wh...
c# expression-trees generics linq
asked by Vamsi

Convert Func<Type, object> expression to Func<T> where T is generic constraint

I have this static function...public static object Create(Type t) { //unimportant } ...I don't have control on the above function above, so I cant alter it. The problem is it's not generic, so I have to cast the returned object to some type. ...This t...
c# expression-trees generics typeconverter
asked by nawfal

create a generic wrapper class which will call the methods with lambda expressions

I am struggling to create a generic wrapper class which will call the methods with lambda expressions....The code looks like this:...The Wrapper Class:...public class Service<T> { private T instance; public Service(T param) { this.ins...
c# expression-trees generics lambda
asked by Alexandru Beu

How to Call a Generic Method using "Expression.Parameter" and "Expression.Call" APIs

Using the ...Expression Tree API... I want to generate code for code that looks like this:...FieldInfo enumFieldInfo = enumFieldInfoSet.SingleOrDefault(fieldInfo => fieldInfo.Name == enumName); ...I have written this code but it's not working:...var enume...
c# expression-trees generics
asked by Tavi Truman

Filtering non-generic DbSet with dynamically built Expression

Plot:...I have a class implemented as a facade around Entity Framework DB context. It developed to maintain backward compatibility, it mimics class with same public interface, but uses DTOs instead of EF entities....Problem:...I have next method inside cl...
dbset entity-framework expression-trees generics linq
asked by Andrey Weber

Page 1 of 1
  • 1

Prime Library

Performance

  • Entity Framework Extensions
  • Entity Framework Classic
  • Bulk Operations
  • Dapper Plus

Expression Evaluator

  • C# Eval Expression
  • SQL Eval Function
More Projects...
Get monthly updates by subscribing to our newsletter!
SUBSCRIBE!