lambda

Is there a better way to express a parameterless lambda than ()=> ?

The () seems silly. is there a better way? For example: ExternalId.IfNotNullDo(()=>ExternalId=ExternalId.Trim()); ...

Grouping List by the last Char

Hi, As you can see on the picture above i have a List "keys" filled with Request.Form.AllKeys which a key starts with "txt". I want to group them by their last id, for example [0] "txtTitle:2" "txtDescription:2" [1] "txtTitle:3" "txtDescription:3" How can i achive this with lambda expression. Thank you very much ...

Lambda ForEach() Index Position

This is somewhat asp.net MVC related only for example purposes but I was hoping to achieve something like this: new SelectList(ViewData.Model.Formats.ToList().ForEach(x => index + " - " + x.Name), "ID", "Name"); Basically trying to be smart and return "index" as a number 1 - n where n is the number of items in the list ViewData.Model....

Correct use of Lambda query.

Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine("List of processes @ " + d.ToString()); Process[] localAll = Process.GetProcesses().Where(o => o.ProcessName.ToLower() != "svchost"); if(localAll.Length > 0) { ...

The proper way to end a BeginInvoke?

I recently read this thread on MSDN. So I was thinking of using a lamda expression as a way of calling EndInvoke just as a way to make sure everything is nice and tidy. Which would be more correct? example 1: Action<int> method = DoSomething; method.BeginInvoke(5, (a)=>{method.EndInvoke(a);}, null); Example 2: Action<int> method ...

How do I invoke a method through reflection with a lambda expression as a parameter?

I want to do this: MethodInfo m = myList.GetType().GetMethod("ConvertAll", System.Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof(object)); List<object> myConvertedList = (List<object>)m.Invoke(myList, new object[]{ (t => (object)t)}); myList is a generic list of a specific type (unknown to the application), and I want ...

LINQ with subselect and groupby to get only the latest version of each item in a list.

I'm a newbie when it comes to LINQ... I have an IEnumerable generic list that contains answers with different versions (each with an FK to the question). From this list I need to get a dictionary of latest version answers only. A very simplified class diagram: Question -ID -question - ...other properties Answer -ID -Version -Questio...

EventHandlers and Anonymous Delegates / Lambda Expressions

I'm hoping to clear some things up with anonymous delegates and lambda expressions being used to create a method for event handlers in C#; for myself at least. Suppose we have an event that adds either an anonymous delegate or lambda expression [for you lucky crowds that can use newer versions of .NET]. SomeClass.SomeEvent += delegate...

Calling a Method from an Expression

What types and arguments does the method, "Any" when using Expression.Call take? I have an inner and an outer Expression that I would like to use with Any. The expressions are built programatically. Inner (this works): ParameterExpression tankParameter = Expression.Parameter(typeof(Tank), "t"); Expression tankExpression = Expressio...

Delegates and Lambdas and LINQ, Oh My!

As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. Please excuse me if the preamble here is too long. I find myself on a project that involves my needing to learn a number of new (to me) technologies, including LINQ (to OBJECTS and to XML for purposes of this pr...

How to lambda the group by data on a LINQ to Sql results?

I get the data from the database like this. Dim query = From t1 In TBL1 _ Join t2 In TBL2 On t1.ID Equals t2.ID _ Join t3 In TBL3 On t1.ID Equals t3.ID _ Group Join t4 In t1 _ On t1.ID Equals t4.ID _ Into t4_Grp = Group _ Select t1, t2, t3, t4_Gr...

How to extract some data from a collection using lambda/linq to objects?

Hi folks, i have an IList<Animals> farmAnimals; this list has three types, Cows Sheep Chickens how can i remove all the Chickens from the list using a lambda query or linq-to-objects so i have a separate list of chickens and the original list now only has cows and sheep. Do i have to make three lists (the original + 2 new ones, fi...

Can I call a function in a lambda expression?

I would like to do the following but I don't think this will work: .OrderByDescending(s => Score(s)), ... private double Score(Story s) { DateTime now = DateTime.Now; TimeSpan elapsed = now.Subtract(s.PostedOn); double daysAgo = elapsed.TotalDays; return s.Votes.Count + s.Commen...

MethodInfo for EntityCollection instead of Queryable

I am manually creating the equivalent lambda: var function = p => p.Child.Any(c => c.Field == "value"); I have a MethodInfo reference to the "Any" method used with Expressions built in code. MethodInfo method = typeof(Queryable).GetMethods() .Where(m => m.Name == "Any" && m.GetParameters().Length == 2) .Single().MakeG...

Lambda variable names - to short name, or not to short name?

Typically, when I use lambdas, I just use "a, b, c, d..." as variable names as the types are easily inferred, and I find short names to be easier to read. Here is an example: var someEnumerable = GetSomeEnumerable(); var somethingElseList = someEnumerable.Select(a => a.SomeProperty) .OrderBy(a => a...

lambda functions in bash

Is there a way to implement/use lambda functions in bash? I'm thinking of something like: $ someCommand | xargs -L1 (lambda function) ...

C# - Lambda syntax for looping over DataGridView.Rows

What is the correct lambda syntax in C# for looping over each DataGridViewRow of a DataGridView? And as an example lets say the function makes the row .Visible = false based on some value in the Cells[0]. ...

How to tell a lambda function to capture a copy instead of a reference in C#?

I've been learning C#, and I'm trying to understand lambdas. In this sample below, it prints out 10 ten times. class Program { delegate void Action(); static void Main(string[] args) { List<Action> actions = new List<Action>(); for (int i = 0; i < 10; ++i ) actions.Add(()=>Console.WriteLine(i)); foreach (Action a in actio...

How do I create a list of Python lambdas (in a list comprehension/for loop)?

I want to create a list of lambda objects from a list of constants in Python; for instance: listOfNumbers = [1,2,3,4,5] square = lambda x: x * x listOfLambdas = [lambda: square(i) for i in listOfNumbers] This will create a list of lambda objects, however, when I run them: for f in listOfLambdas: print f(), I would expect that i...

Combining two expressions (Expression<Func<T, bool>>)

I have two expressions of type Expression<Func<T, bool>> and I want to take to OR, AND or NOT of these and get a new expression of the same type Expression<Func<T, bool>> expr1; Expression<Func<T, bool>> expr2; ... //how to do this (the code below will obviously not work) Expression<Func<T, bool>> andExpression = expr AND expr2 ...