lambda

How to insert an item to Expression arrary

Hi, How can insert an item to Expression array? For example: some code like Expression<Func<int, bool>>[] exprs; Expression<Func<int, bool>> expr = i => i > 0; exprs.Add(expr); Thanks ...

How to Perform String Lookups in Automapper Based on Destination Property Name?

I'd like to generalize a property lookup from code like this .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.GetValue("FirstName")) .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.GetValue("LastName")) ... (repeated for many properties) to a one-liner, conceptually something like this: // How can I ac...

transform a lambda expression

Hi I have the following code Expression<Func<IPersistentAttributeInfo, bool>> expression = info => info.Owner== null; and want to tranform it to Expression<Func<PersistentAttributeInfo, bool>> expression = info => info.Owner== null; PersistentAttributeInfo is only known at runtime though Is it possible? ...

Creating Dynamic Predicates- passing in property to a function as parameter

I am trying to create dynamic predicate so that it can be used against a list for filtering public class Feature { public string Color{get;set;} public string Weight{get;set;} } I want to be able to create a dynamic predicate so that a List can be filtered. I get few conditions as string values ">","<",">=" etc. Is there a wa...

Extract the lambda from an Expression?

I want to implement this function: Public Function GetFunc(Of TSource, TResult) _ selector As Expression(Of Func(Of TSource, TResult)) _ As Func(Of TSource, TResult) 'Implement here End Function UPDATE I have the following issue, it's a part of a function: Dim obj As Object = value For Each exp In expressions If ob...

Using C++ lambda functions during variable initialisation

I think many of you have this kind of code somewhere: int foo; switch (bar) { case SOMETHING: foo = 5; break; case STHNELSE: foo = 10; break; ... } But this code has some drawbacks: You can easily forget a "break" The foo variable is not const while it should be It's just not beautiful So I started wondering if there was a...

Create fully dynamic where clause with expression tree and execute on IQueryable

At point (3) in my code I have defined a query called query1 in which I defined a .Where lambda expression. This query is in some way dynamic but still contains static elements, it always refers to the Type Employee and its (int) property ClientID. Now I very much like to make the refering to the type and its property dynamic, based on...

Defining a lambda expression with an anonymous type contained within that lambda.

Hi, I'm trying to avoid a dynamic type in my lambda expression for grouping a collection. The type is defined anonymously at compile time (and unambiguously as far as I can tell). I'd rather not define the type as a full-fledged class as I'm only using it a few times in this single method. Sample code: Func<MyData, dynamic> dataGroupi...

Lambda Expressions and searching.

Lets say i have a form which have the following : Name:TextBox Email:TextBox Age:TextBox now i want to Get customers Collection based on this filter textboxs so i want to to use something like : List<customer> customers = getCustomerswhere(c=>c.name == txtName.Text && Email == txtEmail.Text); now of course i dont know which...

Pass Expression as a parameter into a Generic method, and plug the Expression into a CreateCriteria?

I have a generic method that exists in EntityRepository that gets entities by Name, which is defined as follows: public IEnumerable<T> GetEntitiesByName<T>(string searchExpression) where T : class, ISearchableEntity, new() { return _session.CreateCriteria<T>() .Add(LambdaSubquery.Property<Fun...

Func or Predicate to ExpressionTree

Lets say i have : Func<Customer,bool > a = (c) => c.fullName == "John"; now i want to convert to expressiontree any way to do that ? i know i can define it from the first place as expressiontree but the situation i have is different because i must concatenate some lambda expressions first then pass it to a method which take express...

Combining Expressions in an Expression Tree

How can I build an expression tree when parts of the expression are passed as arguments? E.g. what if I wanted to create expression trees like these: IQueryable<LxUser> test1(IQueryable<LxUser> query, string foo, string bar) { query=query.Where(x => x.Foo.StartsWith(foo)); return query.Where(x => x.Bar.StartsWith(bar)); } but by ...

filtering a DataTable using LINQ

Consider a DataTable which contains columns: RefID : string RefName : string RefDate : DateTime The DataTable does not contains any primary key. I have another List<string> named ExcludeMe. I would like to filter my DataTable and exclude all of the rows where values in RefId Column around found in the ExcludeMe list. How ca...

Getting Values from ExpressionTrees

let there be: Expression<Func<Customer, bool>> expression = c => c.Name == "John"; now i get the value by using : string myvalue = ((ConstantExpression) bin.Right).Value; now let there be: string x = "John"; Expression<Func<Customer, bool>> expression = c => c.Name == x; now i understand that string myvalue = ((ConstantExpress...

How do I use lambda expressions to filter DataRows?

How can I search rows in a datatable for a row with Col1="MyValue" I'm thinking something like Assert.IsTrue(dataSet.Tables[0].Rows. FindAll(x => x.Col1 == "MyValue" ).Count == 1); But of course that doesn't work! ...

Querying Entity with LINQ using Dyanmic Field Name

I have created a dynamic search screen in ASP.NET MVC. I retrieved the field names from the entity through reflection so that I could allow the user to choose which fields they wanted to search on instead of displaying all fields in the view. When the search result is Posted back to the controller, I receive a FormCollection containing...

C# Lambda to VB.Net

I'm trying to convert a function in C# to VB.Net 2008 and can't seem to make the Lamda expression work. The code is taken from a neat little C# SMTP server that saves emails to Azure blob storage Any help would be appreciated greatly. public void Run() { var mutex = new ManualResetEvent(false); while (true) ...

Expression Trees Conversion

I have two Classes : class Customer { public string Fullname { get; set; } public string Lastname { get; set; } public int Age { get; set; } } and class CustomerDTO { public string Fullname { get; set; } public string Lastname { get; set; } public int Age { get; set; } } now i have an expressiontree Expressi...

How do I find out the type specified by an Expression?

For example, let's say I have a method that takes the following as a parameter: Expression<Func<T, object>> path How do I determine the type of the 'object' specified in the expression? More specifically, I'd like to determine if it's a collection type (eg. IEnumerable) ...

How to sort a collection based on a subcollection property

I would like to sort a collection based on a subcollection property. //the subcollection public class Salary { public int SalaryId {get;set;} public int SalaryYear {get;set;} public double SalaryValue {get;set;} //this is the field we want to sort the parent collection "Person" } //the main collection public class Person { ...