FAQ: LINQ - When To Use Each Syntax

This community-built FAQ covers the “When To Use Each Syntax” exercise from the lesson “LINQ”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Build Web Apps with ASP.NET

FAQs on the exercise When To Use Each Syntax

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Cheers, I’ve come with a question:

the syntax in the solution looks as follows:

var result = heroes.Select(h => $"Introducing...{h}!");

the solution I came up with looks as follows:

var my = heroes.Select(h => heroes[Array.IndexOf(heroes, h)] = $"Introducing...{h}!");

My question is, how can => replace the location of where I want the array to be edited: heroes[Array.IndexOf(heroes, h)] =

I checked the cheatsheet for Lambda expressions, but I couldn’t find any indication as to the logic behind this, so I don’t think I could reproduce the kind of thinking necessary to come up with the shorter version. I lack some kind of knowledge, I think.

Hi,
so the lambda expression represents each value in a collection. That is the h => h part. You can literally read this as: for each element, that I’m gonna name h, do this…
This is because your var my is actually an IEnumerable of a type string. IEnumerables are a type of collection. Similar but also different from lists. Select() is a method you get when you include LINQ in your namespace. Under the hood, this method ITERATES. It will check and/or do something for every value in the same collection.
So unless you give a condition with Where() it will do this for ALL elements.

So, on the other hand, if you only wanted to give the new introduction to only some of the heroes, not all, you would have to use a chained statement with Where() where you would specify with a condition which heroes would be affected by your action.

2 Likes

The format requested is “Introducing…[HERO NAME]!” which I took literally and put the hero names into brackets and made them capital letters, but that marks the printed result as incorrect for this module. I looked at the solution and realized they wanted “Introducing…Hero Name!” with no brackets and not modified to have capital letters. I wish that were made clear.

8 Likes

Same. I wasted a lot of time trying to figure out what was wrong with the way I had coded it due to that UPPER CASE of HERO NAME - instructions should show an actual example to avoid this confusion/ frustration. I tried with/without the square brackets, with/without spaces etc. before finally looking at the solution.

2 Likes

I had to come to the forum to realize it didn’t want the brackets or the .ToUpper().

(@toastedpitabread), why does this raise an error?

foreach (var heroVar in { heroes2, heroes3 })
{
  foreach (var hero in heroVar)
  {
    Console.WriteLine(hero);
   }
}

I’ll just get the solution for me to pass.

I did the same, glad I came to forums to figure out whats wrongs. Thank you.

First off you have to get each individual list for each foreach loop. Second you are trying to run another foreach loop in your other foreach loop and that means you are trying to take each individual value of a list as a list, and since individual values aren’t lists themself that doesn’t work. or:

foreach (var heroVar in { heroes2, heroes3 }) //you are trying to run a list of two values and get them individually
{
foreach (var hero in heroVar) //you are trying to take each of those individual values as a list, which they aren’t //error
{
Console.WriteLine(hero);
}
}

I suggest:

string heroes = { heroes2, heroes3 }; //you should set up a list instead of making a probably unusable list in foreach loop

//you might also need to reassign another valuable with lambada expression to use

foreach (var heroVar in heroes ) //this will print each one
{
Console.WriteLine(heroVar);
}

Same, this should be more clear

This is great !
And the fact no Codecademy moderators is acknowledging this and modifying the exercice shows how much they care.

Also the report feature is just a frontend without backend at all, completely useless (or is it) :slight_smile:

1 Like