lambda

What is the difference between a 'closure' and a 'lambda'?

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused. And now that we're here, how do they differ from a regular function? ...

Refactor To Eliminate Repetition In Lamba Expression

These two methods exhibit repetition: public static Expression<Func<Foo, FooEditDto>> EditDtoSelector() { return f => new FooEditDto { PropertyA = f.PropertyA, PropertyB = f.PropertyB, PropertyC = f.PropertyC, PropertyD = f.PropertyD, PropertyE = f.PropertyE }; } public static Expre...

Checking for duplicates in a complex object using Linq or Lamda expression

I've just started learning linq and lamda expressions, and they seem to be a good fit for finding duplicates in a complex object collection, but I'm getting a little confused and hope someone can help put me back on the path to happy coding. My object is structured like list.list.uniqueCustomerIdentifier I need to ensure there are no d...

Why is it bad to use a iteration variable in a lambda expression

I was just writing some quick code and noticed this complier error Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable. I know what it means and I can easily fix it, not a big deal. But I was wondering why ...

How to Convert all strings in List<string> to lower case using LINQ?

Hi all, I saw a code snippet yesterday in one of the responses here on StackOverflow that intrigued me. It was something like this: List<string> myList = new List<string> {"aBc", "HELLO", "GoodBye"}; myList.ForEach(d=>d.ToLower()); I was hoping I could use it to convert all items in myList to lowercase. However, it doesn't happen...

Wrapping StopWatch timing with a delegate or lambda?

I'm writing code like this, doing a little quick and dirty timing: var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { b = DoStuff(s); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Surely there's a way to call this bit of timing code as a fancy-schmancy .NET 3.0 lambda rather than (God forbid) cutting ...

Limitations of C# Action lambda code blocks

C# .NET 3.5. I'm trying to understand the intrinsic limitation of the C# Action object. Within the lamda (are those, in fact, lamdas?), we can perform assignments, call functions, even execute a ternary operation, but we can't execute a multi-statement operation. Is this because the single-statement execution is just syntactic sugar f...

How .NET 3.5 (lambdas, Linq) evolved

I remember reading a post about a year or so ago by Scott Hanselman (maybe by scott guthrie) about how Linq evolved. It remember it showing that anyonymous types led to lambda expressions which then led to Linq in some way. I can't seem to find it on google. Does anyone else remember this post? If so, can you please post it? ...

Lambda Expression Tree Parsing

Thanks in advance guys. I am trying to use Lambda Expressions in a project to map to a third party query API. So, I'm parsing the Expression tree by hand. if I pass in a lambda expression like: p => p.Title == "title" everything works. however if my lambda expression looks like: p => p.Title == myaspdropdown.SelectedValue Using ...

Linq/lambda question about .Select (newby learning 3.0)

I am playing with the new stuff of C#3.0 and I have this code (mostly taken from MSDN) but I can only get true,false,true... and not the real value : int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var oddNumbers = numbers.Select(n => n % 2 == 1); Console.WriteLine("Numbers < 5:"); foreach (var x in o...

Sort List<DateTime> Descending

In c# (3.0 or 3.5, so we can use lambdas), is there an elegant way of sorting a list of dates in descending order? I know I can do a straight sort and then reverse the whole thing, docs.Sort((x, y) => x.StoredDate.CompareTo(y.StoredDate)); docs.Reverse(); but is there a lambda expression to do it one step? In the above example, Stor...

Where to practice Lambda function?

I am currently trying to learn all new features of C#3.0. I have found a very nice collection of sample to practice LINQ but I can't find something similar for Lambda. Do you have a place that I could practice Lambda function? Update LINQpad is great to learn Linq (thx for the one who suggest) and use a little bit Lambda in some expre...

IQueryable<T> vs IEnumerable<T> with Lambda, which to choose?

I do more and more exercise with Lambda but I do not figure out why sometime example use .AsQueryable(); that use the IQueryable and sometime it omit the .AsQueryable(); and use the IEnumerable. I have read the MSDN but I do no see how "executing an expression tree" is an advantage over not. Anyone can explain it to me? ...

What do you think of multiline lambdas in VB 10

I was just watching a video on MSDN Channel 9 which can be found here, about some of the new features in Visual Basic 10. Now I like most of the new features, some of which have been long awaited(auto properties and Collection Initializers), one that caught my eye was the multiline lambdas like in C#. In the video he used an example ...

C# Action lambda limitation

Why does this lambda expression not compile? Action a = () => throw new InvalidOperationException(); Conjecture is fine, but I would really appreciate references to the C# language specification or other documentation. And yes, I know that the following is valid and will compile: Action a = () => { throw new InvalidOperationExceptio...

Approaching STL algorithms, lambda, local classes and other approaches.

One of the things that seems to be necessary with use of STL is a way to specify local functions. Many of the functions that I would normally provide cannot be created using STL function object creation tools ( eg bind ), I have to hand roll my function object. Since the C++ standard forbids local types to be used as arguments in templa...

Lambdas and the ternary operator, weird issue.

Ok, here's the deal. Generally, when using the ternary, here's the syntax: int x = 6; int y = x == 6 ? 5 : 9; Nothing fancy, pretty straight forward right? Now, let's try to use this when assigning a Lambda to a Func type. Let me explain: Func<Order, bool> predicate = id == null ? p => p.EmployeeID == null :p => p.EmployeeID == id; ...

C# 3.0 Func/OrderBy type inference

So odd situation that I ran into today with OrderBy: Func<SomeClass, int> orderByNumber = currentClass => currentClass.SomeNumber; Then: someCollection.OrderBy(orderByNumber); This is fine, but I was going to create a method instead because it might be usable somewhere else other than an orderBy. private int ReturnNumber(So...

Lambda Expressions in Delphi Prism/Oxygene

I have been experimenting with Lambda expressions in Oxygene. Very simple recursive lambda expression to calculate a fibonacci number : var fib : Func<int32, int32>; fib := n -> iif(n > 1, fib(n - 1) + fib(n - 2), n); fib(3); When I run this code I get a nullreferenceexception. Any ideas as to what I'm doing wrong? ...

Will this WCF client side code cause a memory leak?

One of the frequent causes of memory leaks in .Net are event handlers which are never removed from their source objects. Will this WCF code cause a memory leak, or will the lamda go out of scope too, allowing both the proxy class and the handler to be GCed? void AMethod() { WCFClient proxy; proxy = new WCFClient(); proxy.R...