Can a C# Lambda Expression Have More Than One Statement

Can a C# lambda expression have more than one statement?

Sure:

List<String> items = new List<string>();

var results = items.Where(i =>
{
bool result;

if (i == "THIS")
result = true;
else if (i == "THAT")
result = true;
else
result = false;

return result;
}
);

Lambda/Action with more than one statement in C#. Is it possible?

You can do multi-line lambdas like so:

arguments.ForEach( e => {
e.shouldBePushedToStack = true;
scope.addChild(e));
});

Note that in order to return a value from a multiline lambda, you must use the return keywork. As a trivial example:

arguments.Select( e => {
if (e.shouldBePushedToStack) {
return "foo";
} else {
return "baz";
}
});

However, given that your code has side-effects (modifying e, and appending to an external list), it may be more clear to explicitly use a foreach loop and avoid LINQ, since (as far as I know), LINQ implies you're doing functional-style transformations, which implies there are no side-effects from running the operation.

How can I combine multiple statement in lambda expression

You can't create a lambda expression, since you're not returning anything. You can however create a statement lambda:

Action<string> custom = (name) =>
{
lstCutomers.Add(new Customer(name, coutries[cnt]));
name = name + " Object Created";
};

multiple sentences in a Lambda expression

You have to use braces if you have more then one statement

AddNumber method = r => 
{
Console.WriteLine(r + r);
Console.Read();
};

Is it possible to do several operation within Lambda?

Yes, you just make it a multi line statement inside a code block, with a return

Your this:

.GroupBy(x => x.Name, (y, z) => new { Name= y, Count = z.Count() })

Is like this:

.GroupBy(x => x.Name, (y, z) => {
return new { Name= y, Count = z.Count() };
})

So you can:

.GroupBy(x => x.Name, (y, z) => {
int i = SomeCalc();
var r = new { Name= y, Count = z.Count(), Result = i };
return r;
})

C# multi-line lambda expression

object.ProgressChanged += (sender, args) => {
maxPercent = Math.Min(maxPercent, (int)args.ProgressPercentage);
};

As Jeff pointed out you should use { } for a multi-line lambda.

Multiple Where clauses in Lambda expressions

Can be

x => x.Lists.Include(l => l.Title)
.Where(l => l.Title != String.Empty && l.InternalName != String.Empty)

or

x => x.Lists.Include(l => l.Title)
.Where(l => l.Title != String.Empty)
.Where(l => l.InternalName != String.Empty)

When you are looking at Where implementation, you can see it accepts a Func(T, bool); that means:

  • T is your IEnumerable type
  • bool means it needs to return a boolean value

So, when you do

.Where(l => l.InternalName != String.Empty)
// ^ ^---------- boolean part
// |------------------------------ "T" part

How to set multiple values in a list using lambda expression?

Well personally I wouldn't write the code that way anyway - but you can just use a statement lambda:

A statement lambda resembles an expression lambda except that the statement(s) is enclosed in braces

The body of a statement lambda can consist of any number of statements; however, in practice there are typically no more than two or three.

So the ForEach call would look like this:

.ForEach(x => {
x.BtnColor = Color.Red.ToString();
x.OtherColor = Color.Blue.ToString();
});

I would write a foreach loop instead though:

var itemsToChange = objFreecusatomization.AllCustomizationButtonList
.Where(p => p.CategoryID == btnObj.CategoryID
&& p.IsSelected
&& p.ID == btnObj.ID);

foreach (var item in itemsToChange)
{
item.BtnColor = Color.Red.ToString();
item.OtherColor = Color.Blue.ToString();
}

(You can inline the query into the foreach statement itself, but personally I find the above approach using a separate local variable clearer.)



Related Topics



Leave a reply



Submit