C# guide LINQ Select is wrong

The code in the C# LINQ Select Guide is wrong:

var fruits = new List<Fruit>() {
    new Fruit() { Id = 1, Name = "Orange",     Color = "Orange", Quantity: 3   }, // Quantity: 3 won't compile (colon ':' should be equals '=')
    new Fruit() { Id = 2, Name = "Strawberry", Color = "Red",    Quantity: 12  },
    new Fruit() { Id = 3, Name = "Grape",      Color = "Purple", Quantity: 25  },
    new Fruit() { Id = 4, Name = "Pineapple",  Color = "Yellow", Quantity: 1   },
    new Fruit() { Id = 5, Name = "Apple",      Color = "Red",    Quantity: 5   },
    new Fruit() { Id = 6, Name = "Mango",      Color = "Yellow", Quantity: 2   }

// fruits.Select(f => f.Quantity) returns IEnumerable<int>, which doesn't have a valid Name property inside FirstOrDefault
var mangoQnt = fruits.Select(f => f.Quantity).FirstOrDefault(f => f.Name == "Mango"); // 2

// same situation here, fruits.Select(f => f.Color) returns IEnumerable<string>
var grapeColor = fruits.Select(f => f.Color).FirstOrDefault(f => f.Name == "Grape"); // Purple

So why it’s still : instead of = ?

Also please provide full code, and also what is the problem? what do you expect, and what you get instead ?

Sorry, updated the topic, I meant the LINQ Select on freeCodeCamp Guide under C#

Well I just tried to compile the code using an online compiler, and it stated the same issue (: instead of =)

you may simply assume it’s a typo mistake dear, nothing especial. I might open an issue about it and ask contributors to fix(thanks for report)

I’m not a C# expert, but I think you can’t just copy and paste that sample code and take a run!

Because I think that List comes from a namespace should be imported. Same type Fruit should be defined. Beside note the code might be a .Net 4.5 source level, so it may fail by lower version of .Net

This code is just a sample, it’s more about a partial reference. Don’t know if the full code is available somewhere to test or not.

Thanks again for reporting this, I hope this part of code get fixed soon. I’m not a C# expert, but I would provide some full working code about it of course.

Sure, the ‘:’ instead of ‘=’ is a typo, but the LINQ part isn’t, Select returns an IEnumerable where T is the selected item type:

// this is OK
IEnumerable<int> quantities = fruits.Select(f => f.Quantities);

// this is NOT OK as 'f' is of type 'int', and 'int' doesn't have a property called 'Name'
quantities.FirstOrDefault(f => f.Name == "Mango");

The proper LINQ should be:

// querying the fruit first, and then selecting its quantity returns 2 effectively
var mangoQnt = fruits.FirstOrDefault(f => f.Name == "Mango").Select(f => f.Quantity);  // 2

// querying the fruit first, and then selecting its color returns "Purple" effectively
var grapeColor = fruits.FirstOrDefault(f => f.Name == "Grape").Select(f => f.Color); // "Purple"

SUPERB! Great catch. Well if you ask me, I think this issue is not very critical since FCC focus more on client web stuffs, but thanks for reporting, great catch.

Yes you are right. I’m going to report it to FCC github repository maybe Sunday and included your report, and this thread, you would do it too.

This is just great people help people here, and thanks again for your help and report.

As I’m a C and former Java dev, I might have some review about relevant sections to see any issue to report for them too.

Keep going on great work, happy programming.

The FCC Guide is extremely easy to contribute to.