lambda

How to capture static or non static property by using lambda expression?

There are a lot of benefits for using lambda expression to capture property or method of some class like the following code. void CaptureProperty<T, TProperty> (Func<T, TProperty> exp) { // some logic to keep exp variable } // So you can use below code to call above method. CaptureProperty<string, int>(x => x.Length); However, the...

C# Lambda Conversion to VB

Hi I have been looking at certain tutorials on sharparchitecture and trying to no avail (the online convertors don't seem to be able to do this): private Action<AutoMappingExpressions> GetSetup() { return c => { c.FindIdentity = type => type.Name == "Id"; }; } private Action<IConventionFinder> GetConventions() { ret...

How do I add a list object to class using lambda?

Say I have a class with 2 properties class TestClass { public int propertyOne {get;set;} public List<int> propertyTwo {get; private set;} public TestClass() { propertyTwo = new List<int>(); } } Using linq, I am trying to create a list of TestClass as follows: var results = from x in MyOtherClass ...

Linq Lambda Expression

I am trying to do this on a method which is basically a mapper - maps old categories List to a new List. The OldCategory has fewer properties. return categories = from c in oldCategories select new Category { CategoryName = c.CategoryName, Id = c.CategoryId, Teams = CombineTeam(c.Team, coreTeam) }; Why can't I use CombineTeam met...

Can Expression<Func<T,object>> and destination.x=source.x be refactored?

I'm generating an interface to concrete implementation copier. A later step will be to determine if I can easily add varying behaviors depending on the copy type requested (straight copy, or try, or try-catch-addToValidationDictionary). This is the main one I need/am working on is the try-catch-addToValidationDictionary. It would be love...

How do i know when a lambda expression is null

I need to programatically check whether a nested property/function result in a lambda expression is null or not. The problem is that the null could be in any of the nested subproperties. Example. Function is: public static bool HasNull<T, Y>(this T someType, Expression<Func<T, Y>> input) { //Determine if expression has a nul...

how to cache a lambda in c++0x ?

Hello, I'm trying to work with lambda's in C++ after having used them a great deal in C#. I currently have a boost tuple (this is the really simplified version). typedef shared_ptr<Foo> (*StringFooCreator)(std::string, int, bool) typedef tuple<StringFooCreator> FooTuple I then load a function in the global namespace into my FooTuple...

Using Closures to keep track of a variable: Good idea or dirty trick?

Ok, i have a need to be able to keep track of value type objects which are properties on another object, which cannot be done without having those properties implement an IObservable interface or similar. Then i thought of closures and the famous example from Jon Skeet and how that prints out 9 (or 10) a bunch of times and not an ascendi...

How to perform an update using Linq or Lambda?(C#, Asp.net,Linq , Lambda)

How can I do an update in Linq My code is List<Cart> objNewCartItems = (List<Cart>)Session["CartItems"]; if ((objNewCartItems != null) && (objNewCartItems.Count > 0)) { for (int i = 0; i < dgShoppingCart.Rows.Count; i++) { Cart c = new Cart(); ...

Strongly typed mapping. Lambda Expression based ORM

What do you think of the following table mapping style for domain entities? class Customer { public string Name; } class Order { public TotallyContainedIn<Customer> Parent { get { return null; } } public HasReferenceTo<Person> SalesPerson { get { return new HasReferenceTo<Person>(0,1); } } } //... [TableOf(typeof(Customer))] clas...

C# Linq Result ToDictionary Help

I have a lambda expression that gets results from a Dictionary. var sortedDict = (from entry in dctMetrics orderby entry.Value descending select entry); The expression pulls back the pairs I need, I can see them in the IDE's debug mode. How do I convert this back a dictionary of the same type as ...

Linq - OrberBy the minimum value of two colums

I have an entity set that contains two DateTime properties. I want to order the set by the minimum value of the two properties. How can I do this with a lambda expression. ...

Lambda expression for Entity Framework query

I'm stuck trying to map this sql: select dt.Id, dateadd(dd,datediff(dd,0,dt.CreatedAt),0) as Ct, DId, Amount from dt, ad where dt.ADId = ad.ADId and ad.Id = '13B29A01-8BF0-4EC9-80CA-089BA341E93D' order by dateadd(dd,datediff(dd,0,dt.CreatedAt),0) desc, DId asc Into an Entity Framework-compatible lambda query expression. I'd appreciate...

Lambda expression how to perform String.Format on List<String>?

I have a list like: List<String> test = new List<String> {"Luke", "Leia"}; I would like to use something like this: test.Select(s => String.Format("Hello {0}", s)); but it doesn't adjust the names in the list. Is there a way to use lambda expressions to alter these? Or is it because strings are immutable that this doesn't work? ...

Help with lambdas in Ruby

Hi, I'm new to Ruby and am trying to pass a sort_by lambda to a format method, like this: sort_by_methods = [ lambda {|l, r| compare_by_gender_then_last_name(l, r)}, lambda {|l, r| compare_by_something_else(l, r)}, lambda {|l, r| compare_by_another(l, r)}] formatted_output = "" sort_by_methods...

Write a lambda or anonymous function that accepts an out parameter

I have a delegate defined in my code: public bool delegate CutoffDateDelegate( out DateTime cutoffDate ); I would like to create delegate and initialize with a lambda or anonymous function, but neither of these compiled. CutoffDateDelegate del1 = dt => { dt = DateTime.Now; return true; } CutoffDateDelegate del2 = delegate( out dt ) {...

Why don't anonymous delegates/lambdas infer types on out/ref parameters?

Several C# questions on StackOverflow ask how to make anonymous delegates/lambdas with out or ref parameters. See, for example: Calling a method with ref or out parameters from an anonymous method Write a lambda or anonymous function that accepts an out parameter To do so, you just need to specify the type of the parameter, as in: p...

Using the magic of LINQ - How to call a delegate for each criteria that matches?

Hi! I wanna do something like this: List<string> list = new List<string>(); ... put some data in it ... list.CallActionForEachMatch(x=>x.StartsWith("a"), ()=> Console.WriteLine(x + " matches!");); Syntax: CallActionForEachMatch(Criteria, Action) How is this possible? :) ...

How can one "scan" a lambda expression in C#?

Is is possible to scan a function provided as a lamba expression to figure out the nature of the function at runtime? Example: class Program { static void Main(string[] args) { Examples example = new Examples(x => x ^ 2 + 2); } } public class Examples { public Examples(Func<dynamic, dynamic> func) { ...

Nested Scopes and Lambdas

def funct(): x = 4 action = (lambda n: x ** n) return action x = funct() print(x(2)) # prints 16 ... I don't quite understand why 2 is assigned to n automatically? ...