FirstOrDefault - lucyberryhub/WPF.Tutorial GitHub Wiki

๐Ÿ’ FirstOrDefault

Hello, darling! ๐Ÿ“ Iโ€™m Lucy Berry, your Berry-Tastic Guide to all things coding and cuteness! Today, we're going to learn how to search for a cute cherry in a bunch of berries (aka modifying the DataContext in a lovely way with the FirstOrDefault method)! ๐Ÿ’โœจ

Letโ€™s get ready for a super detailed, step-by-step, and fun tutorial that will show you how to search for a cherry and modify its value, just like finding the juiciest berry in a basket! ๐Ÿ“๐Ÿ’

Letโ€™s do this with cherries and berries while making everything look as cute as possible! ๐Ÿ’๐Ÿ“


Step 1: The Cherry Button ๐Ÿ’

Imagine that you have a basket full of sweet little cherries ๐Ÿ’, and you want to click on one to change its image! First, we need a Button to represent the cute cherry. When you click on this cherry, weโ€™ll change it and also look for another cherry to update. ๐Ÿ“

In this adorable function, weโ€™ll be clicking on the Delete Cherry button ๐Ÿ’ and making two things happen:

  1. Change the clicked cherryโ€™s image.
  2. Find another cherry (with Id + 6) and update its image too!

Here is how weโ€™re going to do this, step-by-step:


Step 2: Code Explanation ๐Ÿ’๐Ÿ“

Letโ€™s look at the code! (Don't worry, itโ€™s all cherry related! ๐Ÿ’)

private void OnDeleteCherryButtonClick(object sender, RoutedEventArgs e)
{
    // Step 1: Check if the sender is a button and if its DataContext is a cherry (a cute berry object!)
    if (sender is Button cherryButton && cherryButton.DataContext is CherryBerryModel selectedCherry)
    {
        // Step 2: Change the image of the clicked cherry ๐Ÿ’
        byte[] cherryImageBytes = Convert.FromBase64String(AppDbConfig.CherryPlusImage);
        selectedCherry.Image = new BerryBlob(cherryImageBytes);  // Give it a cute new image!

        // Step 3: Look for another cherry with the ID that's 6 more than the current cherry ๐Ÿ’
        var targetCherry = cherryBasket.FirstOrDefault(cherry => cherry.Id == selectedCherry.Id + 6);

        // Step 4: If we find the cherry, change its image to a cute empty image ๐Ÿ“
        if (targetCherry != null)
        {
            byte[] emptyCherryImageBytes = Convert.FromBase64String(AppDbConfig.EmptyCherryImage);
            targetCherry.Image = new BerryBlob(emptyCherryImageBytes);  // Give it an empty image!
        }
    }
}

Step 3: Letโ€™s Break Down the Code! ๐Ÿ’

Step 1:
We start by checking if the sender (the thing that triggered the event) is a Button (a cute cherry button ๐Ÿ’). We also check if the DataContext of the button is a cherry object (letโ€™s call it CherryBerryModel). If these things are true, it means we clicked on a cute cherry, and we can change its image! ๐Ÿ“

  • sender is Button cherryButton: We're checking if we clicked on a button (the cherry).
  • cherryButton.DataContext is CherryBerryModel selectedCherry: This checks if the data tied to the button is a cherry berry object (CherryBerryModel).

Step 2:
Now, letโ€™s give our selected cherry a lovely new image! ๐Ÿ’ Weโ€™ll get the image from the AppDbConfig and convert it to a byte array (because the image is stored in base64 format, like a yummy candy). Then, we set this byte array to the selected cherryโ€™s image. โœจ

  • Convert.FromBase64String(AppDbConfig.CherryPlusImage): This converts the cherry image from base64 into byte data.
  • selectedCherry.Image = new BerryBlob(cherryImageBytes): This assigns the new image to the cherry object using a BerryBlob (a special container for images!).

Step 3:
Next, we want to search for another cherry in our basket ๐Ÿ“ (or collection) to update its image. We use the FirstOrDefault method to find the first cherry in the basket that has an ID that is 6 more than the current cherryโ€™s ID! ๐Ÿ’ If we find that cherry, weโ€™ll change its image to a new image that is empty. ๐Ÿ“

  • var targetCherry = cherryBasket.FirstOrDefault(cherry => cherry.Id == selectedCherry.Id + 6): This searches for a cherry with Id = selectedCherry.Id + 6.

Step 4:
If we find that special cherry ๐Ÿ’, we change its image to an empty image (for when a cherry is no longer special). ๐Ÿ“

  • Convert.FromBase64String(AppDbConfig.EmptyCherryImage): This loads the empty cherry image.
  • targetCherry.Image = new BerryBlob(emptyCherryImageBytes): This assigns the empty image to the found cherry.

Step 4: Putting It All Together ๐Ÿ’

Now that weโ€™ve learned the basics, letโ€™s put everything in the cherry jar and make sure the code is super sweet and professional:

private void OnDeleteCherryButtonClick(object sender, RoutedEventArgs e)
{
    if (sender is Button cherryButton && cherryButton.DataContext is CherryBerryModel selectedCherry)
    {
        // Change the selected cherry's image to a "Plus" cherry
        byte[] cherryImageBytes = Convert.FromBase64String(AppDbConfig.CherryPlusImage);
        selectedCherry.Image = new BerryBlob(cherryImageBytes);

        // Find the cherry with the ID + 6 and change its image to "Empty" cherry
        var targetCherry = cherryBasket.FirstOrDefault(cherry => cherry.Id == selectedCherry.Id + 6);
        
        // If the cherry exists, set the image to "Empty"
        if (targetCherry != null)
        {
            byte[] emptyCherryImageBytes = Convert.FromBase64String(AppDbConfig.EmptyCherryImage);
            targetCherry.Image = new BerryBlob(emptyCherryImageBytes);
        }
    }
}

Step 5: Your Cherry Berry Journey ๐Ÿ’

Congratulations, sweetie! ๐Ÿ“ Youโ€™ve just learned how to search for and modify cherries in your basket (or your DataContext). You now know how to change the image of the cherry you clicked and even update another cherry by its Id + 6! ๐Ÿ’ Isnโ€™t that just berry fun and cute? ๐Ÿ’–