lambda

Retrieving lambda expressions from a collection

I'm very new to lambda expressions in C#, and I'm having trouble conceptualizing how they are stored/retrieved in a collection. I'm trying to programatically create a list of 10 Funcs x => x + 1, x => x + 2, etc. as a test. My desired output is 0,1,2,3,4,5,6,7,8,9 Here is my code for that: var list = new List<Func<int, int>>(); for ...

call a function for each value in a generic c# collection

I have a collection of integer values in a List collection. I want to call a function for each value in the collection where one of the function's argument is a collection value. Without doing this in a foreach loop... is there a way to accomplish this with a lambda/linq expression? something like... myList.Where(p => myFunc(p.Value));...

Exception from Lambda Expressions

Strange one that i don't still get, is this: Say, try { stateClient.Socket.BeginSend(messagePrefixed, 0, messagePrefixed.Length, SocketFlags.None, ar => stateClient.Socket.EndSend(ar), stateClient); } catch (SocketException ex) { // Handle SocketException. } catch (ObjectDisposedException ex) { // Handle ObjectDispo...

Limit RemoveAll to a certain number of objects

I am working with a List<T> which contains both parent and children objects. In this list children objects are aware of their related parent object and vice versa. Using this list I am trying to implement a business rule where up to 4 children objects will be removed from the list when their parent is of a certain type. Put differently i...

dynamically calling Max in a linq expression.

Hi, I am trying to call Max, but am getting and error in CreateQuery saying that expression does not implement IQueryable<int> What am I doing wrong here. DataContext dc = new DataContext(Connection.ConnectionString); typeof(Catalog).AssemblyQualifiedName.Dump(); Dictionary<string, IQueryable> dict = new Dictionary<string, IQueryable>...

Entity Framework using Generic Predicates

I use DTO's to map between my Business and Entity Framework layer via the Repository Pattern. A Standard call would look like public IClassDTO Fetch(Guid id) { var query = from s in _db.Base.OfType<Class>() where s.ID == id select s; return query.First(); } Now I wish to pass in filtering criteria from the bu...

Lambda Tutorial and Solving a Lambda-Function

Is it possible to shorten the following function to a lambda expression? Or (to do the trick by myself) what is the best and most understandable for beginners tutorial for lambda in vb.net? Function getit(ByVal wert As Integer, ByVal sk As Integer, ByVal list As List(Of Array)) As String Dim ergebnis As String ergebnis = "Null"...

Why is "Evaluation of lambda expressions is not valid in the debugger"?

I have a seemingly innocent code snippet, which I typed into Visual Studio 2008 Immediate Window. ? results.Join(lstDocs, Function(docID) docID) Evaluation of lambda expressions is not valid in the debugger. As the message says, it's not possible to call a lambda expression in the debugger. Why is it not allowed? ...

What use is lambda in PHP?

The lambda anonymous function is part of PHP 5.3. What use is it? Are there some things that one can only do with lambda? Is lambda better certain for some tasks? I've seen the Fibonacci example, and I really don't need to write Fibonacci sequences, so I'm still not sure if it's that useful for the kinds of tasks I encounter in writing...

convert a list of objects from one type to another using lambda expression

I have a foreach loop reading a list of objects of one type and producing a list of objects of a different type. I was told that a lambda expression can achieve the same result. var origList = List<OrigType>(); // assume populated var targetList = List<TargetType>(); foreach(OrigType a in origList) { targetList.Add(new TargetType(...

How to build a Predicate or Criteria builder for your DAL that understands lambdas ?

I am working on a Data Access Layer Design, we have not finalized what ORM we are going to use as of yet. I am leaning towards NHibernate + FluentMappings + Nhibernate.Linq but depending on project timelines we could even wait for EF4. I want to replace methods like : IList<Customer> FindById(int id); IList<Customer> FindByName(string ...

How does this max() expression in Python work?

Here's the code: a = [1,2,3,4] b = {} b[1] = 10 b[2] = 8 b[3] = 7 b[4] = 5 print max(a,key=lambda w: b[w]) This prints out 1. I don't understand how max(a,key=lambda w: b[w]) is being evaluated here though; I'm guessing for each value i in a, it finds the corresponding value b[i] by saving the current value of i as w in the lambda ...

Python lambdas and scoping

Given this snippet of code: funcs = [] for x in range(3): funcs.append(lambda: x) print [f() for f in funcs] I would expect it to print [0, 1, 2], but instead it prints [2, 2, 2]. Is there something fundamental I'm missing about how lambdas work with scope? ...

Detecting that a MemberExpression has a value

How do I detect if a MemberExpression has a value that needs to be compiled/evaluated? I have two separate member expression outputs, the first which has a value, and the second which doesn't. What is the best way to differentiate between the two? exp **{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6)....

F#: Is the "fun" keyword necessary?

I was typing the "fun" keyword and then I remembered you don't have to in C# Wouldn't this: List.map (x -> x + 1) [1..10] Be just as expressive as this?: List.map (fun x -> x + 1) [1..10] This makes me curious as to why the "fun" keyword is necessary at all. Can someone clarify why the "fun" keyword is syntactically required? ...

Bind Vs Lambda?

Hi, I have a question about which style is preferred: std::bind Vs lambda in C++0x. I know that they serve -somehow- different purposes but lets take an example of intersecting functionality. Using lambda: uniform_int<> distribution(1, 6); mt19937 engine; // lambda style auto dice = [&]() { return distribution(engine); }; Using bind...

Returning different types of arrays from a lambda expression in F#

i have a List of records type Item = { Color : string; Size : int} let itemList = [{Color="Red"; Size=1}; {Color="Green"; Size=2}; {Color="Blue"; Size=3};] I am looking to get turn my list of records into an array of values like [|"Red";"Green";"Blue"|] or [|1;2;3|] I can sorta get there like this typ...

Lambda expression Where on navigation property

Good afternoon, I have three entities (that concern this question) Company (ID, etc..) CompanyAddress (AddressID, CompanyID, Rank) AddressDetails (AddressID, Street, City, State, Zip) The reason Rank and company id aren't in the AddressDetails is because the address detas are shared with contacts via a ContactAddress entity. Anyway, ...

Understanding Lambda

X = 5 L = list(map(lambda x: 2**X, range(7))) print (L) ... I'm expecting this to return: [1, 2, 4, 8, 16, 32, 64] ...instead, it returns: [32, 32, 32, 32, 32, 32, 32] What am I doing wrong? ...

access a lambda expression outside of the enclosing scope

I have the following piece of test code and want to access the variable result outside the enclosing lambda expression. Obviously this does not work as result is always null? I have Googled around a bit but seem to got myself more confused. What are my options? RequestResult result = null; RunSession(session => { result = session.Pr...