lambda

C# Lambda Expression Speed

I have not used many lambda expressions before and I ran into a case where I thought I could make slick use of one. I have a custom list of ~19,000 records and I need to find out if a record exists or not in the list so instead of writing a bunch of loops or using linq to go through the list I decided to try this: for (int i = MinX; i ...

Preferred way of defining properties in Python: property decorator or lambda?

Which is the preferred way of defining class properties in Python and why? Is it Ok to use both in one class? @property def total(self): return self.field_1 + self.field_2 or total = property(lambda self: self.field_1 + self.field_2) ...

Generate lambda Expression By Clause using string.format in C# ?

Hi All, I have a method to Generate Expression By Clause as below: internal static Expression<Func<TModel, T>> GenExpressionByClause<TModel, T>(string column) { PropertyInfo columnPropInfo = typeof(TModel).GetProperty(column); var entityParam = Expression.Parameter(typeof(TModel), "e"); // {e} ...

Getting run-time value of a ParameterExpression in a expression tree

I am missing the obvious: How do I access the value of a parameter inside a lambda expression expression tree? Scenario: For a delegate x I dynamically create a lambda expression with an expression tree body which has the same signature as the delegate x. Inside the lamdba's body, I do some validation, checking, logging stuff (this is ...

Lambda expression and Fluent Nhibernate

How should this implementation look like with fluent nhibernate 1.0 rtm public class Repository<T> : IRepository<T> { public IUnitOfWork UnitOfWork { get { return ObjectFactory.GetInstance<IUnitOfWork>(); } } public ISession Session { get { return UnitOfWork.CurrentSession; } } public T Get(Expression<Func<T, bool>> expression) { } } ...

AutoMapper and Linq expression.

I am exposing the Dto generated from AutoMapper to my WCF services. I would like to offer something like that from WCF: IList GetPersonByQuery(Expression> predicate); Unfortunately I need back an expression tree of Person as my DAL doesn't know the DTO. I am trying this wihtout success: var func = new Func<Person, bool>(x => x.F...

lambda expressions in VB.NET... what am I doing wrong???

when I run this C# code, no problems... but when I translate it into VB.NET it compiles but blows due to 'CompareString' member not being allowed in the expression... I feel like I'm missing something key here... private void PrintButton_Click(object sender, EventArgs e) { if (ListsListBox.SelectedIndex > -1) { //Context using ...

How does a lambda in C# bind to the enumerator in a foreach?

I just came across the most unexpected behavior. I'm sure there is a good reason it works this way. Can someone help explain this? Consider this code: var nums = new int[] { 1, 2, 3, 4 }; var actions = new List<Func<int>>(); foreach (var num in nums) { actions.Add(() => num); } foreach (var num in nums) { var x = num; act...

NHibernate Lambda exressions - are they turned into SQL.

Hi, I'm a bit of a NHibernate newbie and Im taking on some code written by another developer. I want to find out how NHibernate converts lambda based criteria into SQL. I know in Linq to SQL using Lambda expressions on queries means that the whole thing is turned into an expression tree and then into SQL (where possible) by the Linq to ...

How can I combine several Expressions into a fast method?

Suppose I have the following expressions: Expression<Action<T, StringBuilder>> expr1 = (t, sb) => sb.Append(t.Name); Expression<Action<T, StringBuilder>> expr2 = (t, sb) => sb.Append(", "); Expression<Action<T, StringBuilder>> expr3 = (t, sb) => sb.Append(t.Description); I'd like to be able to compile these into a method/delegate equi...

Theoretical question about browser javascript engines.

I doubt this is possible, and I'm certain to do it in a production environment would be an impressively bad idea. This is just one of those hypothetical "I-wonder-if-I-could..." things. I was wondering if it is possible to modify or extend how the browser JavaScript engine parses the code at run-time. For example: if I tried to use...

Lambda and Nested Objects

I took a look at this answer and it goes in part to solving my issue. However, what I need is the following. Given I have an object; Product string code List<suitability> items and then i have this object; Suitability key value Each Product has a variable amount of [items] and the key/value pairs differ from product to pr...

how to convert lambda expression to object directly?

I have to do through Action like this: Action action = () => { ..// }; object o = action; any way to do this: object o = () =>{}; //this doesn't compile ...

LINQ: converting a query expression to use lambdas

I'm trying to rewrite a LINQ To Entities query to an expression. My model is a School which can have many Persons. Persons are inherited out to teachers, students, etc. The following query works for me: IQueryable<DAL.TEACHER> teacher = from p in School select p.PERSON as ESBDAL.TEACHER; How would I write this as a query expressio...

How To? Use an Expression Tree to call a Generic Method when the Type is only known at runtime.

Please bear with me; I am very new to expression trees and lambda expressions, but trying to learn. This is something that I solved using reflection, but would like to see how to do it using expression trees. I have a generic function: private void DoSomeThing<T>( param object[] args ) { // Some work is done here. } that I need ...

lambda expressions in C#?

I'm rather new to these could someone explain the significance (of the following code) or perhaps give a link to some useful information on lambda expressions? I encounter the following code in a test and I am wondering why someone would do this: foo.MyEvent += (o, e) => { fCount++; Console.WriteLine(fCount); }; foo.MyEvent -= (o, e) =...

Ruby Design Problem for SQL Bulk Inserter

This is a Ruby design problem. How can I make a reusable flat file parser that can perform different data scrubbing operations per call, return the emitted results from each scrubbing operation to the caller and perform bulk SQL insertions? Now, before anyone gets narky/concerned, I have written this code already in a very unDRY fashion...

Reusing Linq to Entities' Expression<Func<T, TResult> in Select and Where calls

Hi, Suppose I have an entity object defined as public partial class Article { public Id { get; set; } public Text { get; set; } public UserId { get; set; } } Based on some properties of an Article, I need to determine if the article can be ...

Linq - reuse expression on child property

Not sure if what I am trying is possible or not, but I'd like to reuse a linq expression on an objects parent property. With the given classes: class Parent { int Id { get; set; } IList<Child> Children { get; set; } string Name { get; set; } } class Child{ int Id { get; set; } Parent Dad { get; set; } string Name { get; set; } ...

Func<sometype,bool> to Func<T,bool>

If i have: public static Func<SomeType, bool> GetQuery() { return a => a.Foo=="Bar"; } and a generic version public static Func<T, bool> GetQuery<T>() { return (Func<T,bool>)GetQuery(); } Is there a way to cast my strongly typed Func of SomeType to a Func of T? The only way I have found so far is to try and combine it with a mock...