lambda

IEqualityComparer for anonymous type

I have this var n = ItemList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList(); n.AddRange(OtherList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList();); I would like to do this if it where allowed n = n.Distinct((x, y) => x.Vchr == y.Vchr)).ToList(); I tried...

Need Help understand Moq better.

Hi I been looking at the Moq documentation and to me the comments are too short for me to understand each of things it can do. First thing I don't get is It.IsAny<string>(). //example using string Is there an advantage of using this over just putting some value in? Like I know people say use this if you don't care about the value but...

Linq Lamba support in WebForms ASCX

After having worked in MVC for a few months, I'm back in a previously written WebForms 3.5 application, and I'm trying to fix up what I can with what I've learned. Part of this is the "strongly-typed model with a partial view" concept which is incredibly awesome. By inheriting my custom "ListTemplate" control, I can then use its GetMod...

How do I sort a string array alphabetically by length?

By alphabetically by length I mean as follows: given: { "=", "==>>", "=>>", "=>", "!>" } I want to get out: !> = => =>> ==>> I'm currently just using OrderBy(x => x.Length).ToArray() anyone got a better lambda? EDIT: I'm going about this ALL wrong, please vote to close! ...

How to write strongly typed lambda expressions?

I want to write a lambda expression within an inline if statement. But inline if statement must have strong type results. MyType obj = someObj.IsOk ? null : () => { MyType o = new MyType(intVal); o.PropertyName = false; return o; }; Of course this doesn't work, because lambda expression isn't strongly typed. I thought of usin...

C#: How to make objects "assume" they got overload for operator X?

Hi. Is there a way of forcing the C# compiler to ignore missing operator overloads for objects, and instead handle that check in runtime? I ask because I have a container that has multiple objects that has various attributes of type int, string, ushort and so on. I'm making a search function for that container, and would like to be able...

C#: Recursive functions with Lambdas

The below does not compile: Func<int, int> fac = n => (n <= 1) ? 1 : n * fac(n - 1); Local variable 'fac' might not be initialized before accessing How can you make a recursive function with lambdas? [Update] Here are also two links that I found interesting to read: Eric Lippert's "Why does a recursive lambda cause a defini...

Creating a function dynamically at run-time

It probably isn't even possible to do this, but I will ask anyway. Is it possible to create a function that receives a string and then uses it as a right side argument for the goes to operator (=>) used in lambda? Actually, what I want to do is to be able to redefine an specific method of a specific class during runtime. I want to be wr...

If you are a language designer, how would you make ignoring parameters more succint for lambdas?

Based on this, how would you make ignoring parameters more succint? var m = menuStrip.Items.Add("Hello", null, delegate { MessageBox.Show("Como Esta Mundo"); }); I'm thinking along the lines of: var m = menuStrip.Items.Add("Hello", null, ==> MessageBox.Show("Como Esta Mundo") ); var m = menuStrip.Items.Add("Hello", null, ? => Message...

Returning inside for loop inside lambda crashes in Ruby 1.8

In Ruby 1.8 (my version is 1.8.7-72), this code: foo = lambda do for j in 1..2 return end end foo.call crashes with a LocalJumpError: test2.rb:3: unexpected return (LocalJumpError) from test2.rb:2:in `each' from test2.rb:2 from test2.rb:6:in `call' from test2.rb:6 Why does it do this? However, it seems to ru...

What is this 'Lambda' everyone keeps speaking of?

What is this 'Lambda' everyone keeps speaking of? A lot of people seem to love it, but all I can gather from it is it is just a way of cramming lots of lines of code into a single expression. Can someone please enlighten me on its true value? ...

Terminology when copying the captured variable for a lambda/ anon-method

I translated this code(it has bad side effect that it just capture the outer variable): foreach (TaskPluginInfo tpi in Values) { GenerateMenu(menuStrip, tpi.MenuTree, tpi.MenuText, delegate { tpi.ShowTask() }); } To this code(because the above is not working): foreach (TaskPluginInfo tpi in Values) { ...

Lambda to VB question

1- <IEnumerable<int>> exceptionFunction = () => list1.Except(list2); 2- Action displayList = () => exceptionFunction() .ToList() .ForEach(i => Debug.WriteLine(i)); In the code above I am able to translate line 1 to VB with no problem. Everything I try to convert line 2. gives me...

Performance difference with MemberInit Expression

I am working a similar problem as Question 222511 I do need to use the MemberInit Expression so I can just add them to the constructor... I am trying to implement John Skeet's answer but I am running into a big performance difference. Here is some of the code: // Method A: // This work good, is fast and returns an un-executed query... D...

Reading a property value with a lambda expression

I'm writing a file generic block for my application and started with using Lambda expressions for managing my rule sets for block generation to avoid the pitfalls of magic strings, configuration hell etc. Inside my mapping class I have lines similar to: Map(x => x.Name).Length(20).PadLeft(true).PaddingChar("#"); This works fine and i...

No-op lambda

I have an event on one of my classes that I want to attach a handler to. However, I don't need the handler to do anything, as I am just testing the behaviour of the class with handlers attached or not. The event signature is as follows: public event EventHandler<EventArgs> Foo; So I want to do something like: myClass.Foo += (); Ho...

Help me lambda-nize this

To help me better understand lambda I wrote this short snippet that rotates and transforms a quad (I hope I got the math right). Now, I want to replace the three steps below with one liner lambdas, possibly in conjunction with map(). Im using a vector class but hopefully, the functions are clear as to what they do. self.orientation = v...

Can a method be overriden with a lambda function

Is there any way to override a class method with a lambda function? For example with a class definition of class MyClass { public virtual void MyMethod(int x) { throw new NotImplementedException(); } } Is there anyway to do: MyClass myObj = new MyClass(); myObj.MyMethod = (x) => { Console.WriteLine(x); }; ...

Python Lambda Problems

What's going on here? I'm trying to create a list of functions: def f(a,b): return a*b funcs = [] for i in range(0,10): funcs.append(lambda x:f(i,x)) This isn't doing what I expect. I would expect the list to act like this: funcs[3](3) = 9 funcs[0](5) = 0 But all the functions in the list seem to be identical, and be se...

Is this bad design?

I have a control and it provides a selection mechanism. However, the items that it selects are not appropriate for general consumption and instead, must be projected into a different type parameterised by date. In addition to this, there are some controls that want to encapsulate the same information and, more importantly, want to retai...