Kinetic Code Camp - Bring your skills, or lack thereof.. :dumpster_fire:

Statement Lambdas (Snippet)

Back on the subject of lambdas..

We’ve already covered Expression lambdas here, and the posts above and below, so let’s move on to Statement lambdas.

You use statement lambdas, when you need to do more than one statement, or do some more complex logic than an expression statement.

For example:

//using LINQ here (I just make this crap up, who knows if this is remotely correct.)
string someString = Db.PerCon.Where(p => p.PerConID == "ROCK").FirstOrDefault()
    .Select(str =>
    {
        string t1 = p.Name.ToUpper();
        string t2 = p.FirstName.ToLower();
        string t3 = p.LastName.ToUpper();
        if(t3.Length() > t2.Length())
        {
            return "Yo";
        }
        else return "Bollocks";
    }); //I said it was gibberish, but it's logic.

//or an Event, in a form: (try not to do this)
UD01Form.Shown += (sender, eventArgs) =>
{
    someClassLevelVariable = ((Control)sender).Name;
    MessageBox.Show(JsonConvert.SerializeObject(eventArgs, Formatting.Indented));
};

And that’s pretty much it. It’s a method, where you can do what you want in it.

If y’all want more on this one, please ask a specific question, and I’ll see if I can answer it, or prod me with a needed example.

Aha! Got one:

//Returning a list of new object from LINQ AND manipulating the output with a filter.
//This is bs code, don't do this lol
var listOfPeople = Db.PerCon.Where(p => p.LastName.StartsWith("Q"))
    .Select(p =>
    {
        var person = new
        {
             name = p.FirstName + " " +  p.LastName,
             hasMiddle = !String.IsNullOrWhiteSpace(p.MiddleName)
        }; 

        if(person.HasMiddle) return new
        {
             name = p.FirstName + " " + p.MiddleName + " " + p.LastName,
             hasMiddle = !String.IsNullOrWhiteSpace(p.MiddleName)
        }
        else return person;
    }).ToList();

My examples suck today, but I hope it’s clear.