lambda

Entity Framework - Cannot convert lambda expression to type 'string' because it is not a delegate type

I am using Entity Framework in my C# based code. I am running into an unexpected weirdness and am looking for suggestions. Case 1, 2, 3, 4... Projects: RivWorks.dll RivWorks.Service.dll RivWorks.Alpha.dll Samples (all of these work): RivWorks.Alpha.dll: public static bool EndNegotitation(long ProductID) { var product = (from a ...

Expression tree - how to get at declaring instance?

I'm a newbie when it comes to expression trees, so I'm not sure how to ask this question or what terminology to use. Here's an overly-simplifed version of what I'm trying to do: Bar bar = new Bar(); Zap(() => bar.Foo); public static void Zap<T>(Expression<Func<T>> source) { // HELP HERE: // I want to get the bar instance and call...

Using Lambdas as Constraints in NUnit 2.5?

According to Charlie Poole's NUnit blog, it is possible to use Lambda expressions as constraints in NUnit 2.5. I just can't seem to be able to get it to work? I am using NUnit 2.5.3.9345. Using the example lambda from the blog post: [TestFixture] public class Class1 { [Test] public void someTest() { int[] array = {1...

Exception with lambda express

Hello, I have a strange behavior. The code below worked from a long time but nowI don't know why I didn't change anything, I have an exception. I get employee from my database via Nhibernate, the _model.List have the employee list. I have an exception on the line juste before the return (where I build the array). I format the data to us...

how to write recursive lambda functions in c++0x

i am new to c++0x, so please excuse me if my question is silly :). i am writing the following recursive lambda function, it doesn't compile. sum.cpp #include <iostream> #include <functional> auto term = [](int a)->int { return a*a; }; auto next = [](int a)->int { return ++a; }; auto sum = [term,next,&sum](int a, int b)mutable -...

C# Extension methods on "members"

I have some extension methods which could be used like this: MyType myObject; string displayName = myObject.GetDisplayName(x => x.Property); The problem here is that it needs an instance, even if the extension method only needs the type MyType. So if there is no instance, it needs to be called like this: string displayName = BlahBla...

Lambda expression compilation

Hi! Given the lambda expression below where Province type contains a public property "byte CountryId" and Country type which contains a public property "byte Id". Expression<Func<Province, bool>> exp = p => p.CountryId == country.Id; The Expression is later used by NHibernate Linq provider and threw an exception. When I inspected th...

Expression to Call a Method on Each Property of a Class

I want to take a class, loop through it's properties, get the property value, and call a method passing that property value in. I think I can get the property values, but what does the lambda expression's body look like? What body is used to call a method on each property? This is what I have so far... Action<T> CreateExpression<T>( T...

What is the best language independent source to learn Lambdas/Closures?

Most programming languages are utilizing lambdas/closures. Which language agnostic source is recommended as the best to learn Lambda basics? Lambdas/Closures in different languages: Perl Lambdas Python Lambdas .Net LINQ Java Closures Scheme Lisp Php Closure and Lambdas Javascript Closures C++ ML Wikipedia: Closures (computer science)...

Expression Trees in .NET 4.0: Expression.Call fails to find method "get_Item" in type List<T>

Hi there, I'm stuck with the problem below and wondering is someone out there will be able to help. I have added comments to the code to make it self-explanatory but let me know if you need more info or if the problem is unclear. Thanks a lot in advance! Edit: I've been asked to summarize the problem in text, so here it goes: under th...

Lambda Functions in PHP aren't Logical

Note: I have condensed this article into my person wiki: http://wiki.chacha102.com/Lambda - Enjoy I am having some troubles with Lambda style functions in PHP. First, This Works: $foo = function(){ echo "bar"; }; $foo(); Second, This Works: class Bar{ public function foo(){ echo "Bar"; } Third, This works: $fo...

String expression parsing tips?

I got bored during the holiday season this year and randomly decided to write a simple list comprehension/filtering library for Java (I know there are some great ones out there, I just wanted to write it my self for the hell of it). For this list: LinkedList<Person> list = new LinkedList<Person>(); list.add(new Person("Jac...

How to create a method which accepts a lambda expression

So I find myself writing this code all the time: [TestMethod] [Description("Asserts that an ArgumentNullException is thrown if ResetPassword(null) is called")] public void ResetPassword_Throws_ArgumentNullException_With_Null_Parameter( ) { try { new MembershipServiceProvider( ).ResetPassword( null ); } catch ( A...

lambda expressions in c# vs. vb.net

Functionally, is there any difference (apart from syntax onbviously) between lambda expressions in C# and VB.Net? EDIT: following up on CraigTP's answer: any references to the situation in .Net 4? EDIT: I'm asking because I'm used to C#, but for a next project the customer asks VB.Net. We're not a priori against that. We realize that m...

asp.net mvc: make RedirectToAction(string, object) into RedirectToAction<Controller>(x => x.Detail(id))

anybody knows how to make a method (I will put it in a extensions class) that will do the same as the mvc's RedirectToAction only using expressions (no magic strings). So that instead of writing something like this: RedirectToAction("Detail", new RouteValueDictionary { {"messageId", messageId}}); I would do like this: this.Red...

Lambda's in VB.net?

In C# I would simply do this: myIEnumerable.Where(i=>i.ReturnsABool()).any(); How would I do that in VB.net? I'm stuck on how to formulate the lambda.. ...

Is there a good source for Lambda brain teasers

I'm looking for something similar to SQL for Smarties by Joe Celko. More specifically I'm interested in .Net 3.5 or 4.0. ...

Replacing parameters in a lambda expression

I had a part of code that takes in lambda expressions at runtime, which I can then compile and invoke. Something thing; Expression<Action<Something>> expression = (c => c.DoWork()); Delegate del = expression.Compile(); del.DynamicInvoke(thing); In order to save execution time, I stored those compiled delegates in a cache, a Dictionar...

C#: An item with the same key has already been added, when compiling expression

Ok, here's a tricky one. Hopefully there is an expression guru here who can spot what I am doing wrong here, cause I am just not getting it. I am building up expressions that I use to filter queries. To ease that process I have a couple of Expression<Func<T, bool>> extension methods that makes my code cleaner and so far they have been w...

Get Alphabetical List Of Items in Collection Using Lambda/Linq?

I have a list of objects. Each object contains a property called 'DisplayName'. I want to create another list of string objects, where each string represents the first letter or character (could be a number) in the DisplayName property for all objects in the initial list, and I want the list to be distinct. So, for example, if my list...