lambda

Lambda Expression Weirdness in Linq to SQL Where condition

Hi, I am working on an ASP.Net MVC application which uses the repository pattern with linq to sql as my data source. In my repository I expose the following method: public IEnumerable<T> Find(Expression<Func<T, bool>> where) { return _context.GetTable<T>().Where(where); } I am able to call this by saying: repository<User>.Find(u...

What exactly is the point of Expression Trees?

Ok, I just don't get it. I've read about as much as I can on the subject without knowing what it's all about: Why use Expression Trees? What's a real-world example of when and how I'd use them? What overall benefit(s) are there in using them? ...

Lambda Calculus reduction

All, Below is the lambda expression which I am finding difficult to reduce i.e. I am not able to understand how to go about this problem. (λm λn λa λb . m (n a b) b) (λ f x. x) (λ f x. f x) This is what I tried, but I am stuck: Considering the above expression as : (λm.E) M equates to E= (λn λa λb. m (n a b) b) M = (λf x. x)(λ f x....

cross thread invoke compilation problem

Can anyone advise why this line of code would not compile? It generates CS1660 instead: s.run_button.Invoke((b) => { b.Enabled = false; }, new object[] { s.run_button }); Visual studio says: Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type ...

Building a LINQ expression tree: how to get variable in scope

I'm building a LINQ expression tree but it won't compile because allegedly the local variable $var1 is out of scope: variable '' of type 'System.Object' referenced from scope '', but it is not defined This is the expression tree: .Block() { $var1; .If ($n.Property1 == null) { .Block() { $var1 = null; ...

How to pass an expression to a method like a variable?

I have 3 methods that are almost exactly identical: protected DetachedCriteria GetAvailableFundIdsPerDataUniverse() { return GetAvailableIdsPerDataUniverse() .SetProjection(LambdaProjection.Property<Fund>(f => f.Id)); } protected DetachedCriteria GetAvailableCompanyIdsPerDataUniverse() { return GetAvailableIdsPerDataUni...

Causing a method to return from a lambda

I have a fluent validation API that throws an exception when a supplied boolean result is false. This works fine. I'd like to improve upon this by, instead of throwing an exception, providing some functionality to execute if the result is false, and then forcing the calling method to return. Example, the supplied functionality might new...

How to invoke generic lambda expressions?

private void ExecuteCommand(Expression<Func<bool>> command) { bool success = command.Compile().Invoke(); } private void Test() { ExecuteCommand(() => _gc.ChargeCancellation("")); } With this code, I got a NullReferenceException. ...

How to get a method to return an expression to resolve 'Unrecognised method call in epression' error?

I have the following statement that's throwing an error I don't understand: return (int) _session.CreateCriteria<T>() .Add(LambdaSubquery.Property<Fund>(x => x.Id) .In(GetAvailableIdsPerDataUniverse(x => x.GetDataUniverseId()))) .AddNameSearchCriteria<T>(searchExpression) .SetProjection(LambdaProjection.Count<T>(e =>...

Modern C#: how to make a function private to a method

I'm working on a method that needs to repeat a small operation at different spots, but the code to be repeated should be private to the method. The obvious solution is a nested function. Whatever I try however, the C# compiler barfs at me. Something roughly equal to this Perl snippet: my $method = sub { $helper_func = sub { code to...

Why do I get a null reference exception in this expression tree?

I have a tree expression that looks like this: .Block( System.Object $instance, MyType2 $result) { $result = (MyType2)((MyType1)$instance).Property1; .Goto return { }; .Label .LabelTarget useDefault:; $result = .Default(MyType2); .Label .LabelTarget return:; $result } These are the custom types ...

Interop between F# and C# lambdas

F# powerpack comes with a set of conversion methods to translate from Func<...> to F# functions, either standard or tupled ones. But is it possible to achieve the opposite: in case you want to call from F# code a C# method that takes Func<...> and want to use native F# lambda expression (e.g. fun x -> some_function_of(x))? If I send a F...

Why delegates generated dynamically from expressions are slower than hard-coded lambdas ?

I would have expected delegates generated from expression trees to achieve roughly the same performance as hard-coded, static, equivalent anonymous methods. However, it seems that dynamically generated delegates are noticeably slower... Here's a simple test program to illustrate the case. It just accesses 3 properties of an object 10000...

How can I create an Expression within another Expression?

Forgive me if this has been asked already. I've only just started using LINQ. I have the following Expression: public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary() { return m => (new CustomerSummary() { ID = m.ID, CustomerName = m.CustomerName, LastSalesContact = // This is a Per...

Predicates and OrderBy , Func....

i understand that predicates are delegate to function which return bool and take generic param , i understand that when i say mycustomer => mycustomer.fullname == 1 it actually means: delegate (Customer mycustomer) { return mycustomer.fullName == "John"; } the paramter im passing in when i ...

python sort without lambda expressions

I often do sorts in Python using lambda expressions, and although it works fine, I find it not very readable, and was hoping there might be a better way. Here is a typical use case for me. I have a list of numbers, e.g., x = [12, 101, 4, 56, ...] I have a separate list of indices: y = range(len(x)) I want to sort y based on the value...

Convert custom datatable sort to LINQ or lambda expressions

I have a custom sort routine that works great, but I was curious about how I would convert it to use less code with LINQ or lambda. SortByGrade sorts a string value representing a grade ( K - 12 ) so that it returns: K, 1, 2, 3, 4, etc. instead of 1, 10, 2, etc /// <summary> /// Sorts a grade ( K - 12 ) numerically /// </su...

Using GroupBy, Count and Sum in LINQ Lambda Expressions

I have a collection of boxes with the properties weight, volume and owner. I want to use LINQ to get a summarized list (by owner) of the box information e.g. **Owner, Boxes, Total Weight, Total Volume** Jim, 5, 1430.00, 3.65 George, 2, 37.50, 1.22 Can someone show me how to do this with Lambda expression...

Why is lambda instead of function defintion shorthand considered good style in scheme?

In scheme, why is this: (define foo (lambda (x) 42)) considered better style than this: (define (foo x) 42) And is there any reason to favour one over the other? ...

how to order asc/dsc with lambda or linq

how to order descending an IEnumerable<T> with linq or lambda ? ...