lambda

anonymous function usefulness

Why not write the anonymous function content only instead of the anonymous function AND the anonymous function content? ...

When is an anonymous function executed? It doesn't have a name to call!

I know it's not executed immediatly, but then when? ...

How do you know you have to use function() {}?

code 1 $(document).ready(function() { $('.poem-stanza').addClass('highlight'); }); code 2 $(document).ready( $('.poem-stanza').addClass('highlight'); ); ...

What is the shortest code to compare two comma-separated strings for a match?

The method below called UserCanAccessThisPage is based on the following logic: each user and each page has a list of groups. If any of these match, the user has access to the page. The code below does what I want, but my solution is very C# 1 (or C# 2, at least I didn't use ArrayList). Can anyone refactor this so it is more straight-fo...

SubSonic 3 ActiveRecord lambda expression partially ignored on delete

I have tables Users, Widgets and Layouts. Users have many-to-many relationship with Widgets via Layouts. Each Layout has UserID and WidgetID. I want to delete a Layout that matches specific UserID and WidgetID. Using SubSonic 3 ActiveRecord I write: Layout.Delete(x => x.UserID == user.id && x.WidgetID == id); However, SubSonic delete...

C#: Is it possible to use expressions or functions as keys in a dictionary?

Would it work to use Expression<Func<T>> or Func<T> as keys in a dictionary? For example to cache the result of heavy calculations. For example, changing my very basic cache from a different question of mine a bit: public static class Cache<T> { // Alternatively using Expression<Func<T>> instead private static Dictionary<Func<T...

How to merge two C# lamba Expressions without an invoke?

I'd like to merge the following Expression: // example class class Order { List<OrderLine> Lines } class OrderLine { } Expression<Func<Order, List<OrderLine>>> selectOrderLines = o => o.Lines; Expression<Func<List<OrderLine>, Boolean>> validateOrderLines = lines => lines.Count > 0; // now combine those to Expression<Func<Or...

ASP.net MVC Action URLs with lambda expression

I'm sure I have seen this syntax <%= Url.Action((MyController c) => c.MyMethod("a")) %> or something like it as a way to generate action URLs in ASP.net MVCs without magic strings. However, I can't find that Action overload. I have ASP.NET MVC 1.0. Where is it? ...

C# - Finding the common members of two List<T>s - Lambda Syntax

So I wrote this simple console app to aid in my question asking. What is the proper way to use a lambda expression on line 3 of the method to get the common members. Tried a Join() but couldn't figure out the correct syntax. As follow up... is there a non-LINQ way to do this in one line that I missed? class Program { static void Mai...

Does a lambda create a new instance everytime it is invoked?

I'm curious to know whether a Lambda (when used as delegate) will create a new instance every time it is invoked, or whether the compiler will figure out a way to instantiate the delegate only once and pass in that instance. More specifically, I'm wanting to create an API for an XNA game that I can use a lambda to pass in a custom call ...

Compiling a Delegate with Expression.Lambda() - Parameter Out Of Scope, but is it really?

...

How to pass the PropertyInfo of a property as parameter in a attribute?

Lets say I have two classes: class A { [SortOrder(3)] public string Name { get; set; } } class B : A { [SortBefore(*****)] public string Age { get; set; } } Note the stars in the property attribute in the second class. Would it somehow (using expressions I guess) be possible to specify A.Name in the SortBefore attribute? ...

C# cannot implicitly convert type T to type T

my method: public TableFilled<TKey, TRow> getTera() { Func<TablesFilled<TKey,TRow>> _getTera=new Func<TablesFilled<TKey,TRow>>( ()=>{return (TablesFilled<TKey,TRow>) chGetTera();}); //Above does not compile says: Cannot convert type //'AcapsVerify.FunctionalTables.TableFilled<TKey,TRow>' to //'AcapsVerify.F...

Can you reverse order a string in one line with LINQ or a LAMBDA expression

Not that I would want to use this practically (for many reasons) but out of strict curiousity I would like to know if there is a way to reverse order a string using LINQ and/or LAMBDA expressions in one line of code, without utilising any framework "Reverse" methods. e.g. string value = "reverse me"; string reversedValue = (....); an...

Debugging lambda expressions in Visual Studio 2008

Do you know a Visual Studio 2008 Addon, which allows the debugging of Lambda expressions (in the Watch window)? ...

Why such hype with C# lambda functions?

I'm beginning to program in C# 2.0, so I have never used lambda expressions, but, why so much fuss about it? Are them just syntactic sugar around anonymous delegates, or is there something more which I can't see? ...

VB.NET Lambda Expressions

If I have Visual Studio 2008 and I target a .NET 2.0 application, can I still use Lambda Expressions? My understanding of Lambda Expressios is that its a feature built into the compiler, not the framework, so my conclusion would be that I could use Lambda in .NET 2.0 application. Can someone please tell me if this is so? ...

Linq Evaluating a method as a lambda expression

Hi, I am attempting to select from a List using a linq expression where the range variable is evaluated in a static method that returns boolean. I would like to select the range variable that returns true when the range variable is evaluated using the method. var result = from rangeVariable in DataSource where (rangeVariab...

Boolean evaluation in a lambda

Just tooling around for my own amusement, and I want to use a lambda, because I feel like it. Can I replace this function with a lambda? def isodd(number): if (number%2 == 0): return False else: return True Elementary, yes. But I'm interested to know... ...

Recursive non-binary, non-sorted tree search using c# lambas

class TreeNode { public string Value { get; set;} public Collection<TreeNode> Nodes { get; set;} public TreeNode() { Nodes = new Collection<TreeNode>(); } } 1) How would you write the recursive lambda expression to return the TreeNode with a particular value (or null if not found) assuming the values are u...