Learn

Every LINQ query returns either a single value or an object of type IEnumerable<T>. For now, all you need to know about that second type is that:

  • It works with foreach loops, just like arrays and lists
  • You can check its length with Count()

Since the single value type and/or the parameter type T is not always known, it’s common to store a query’s returned value in a variable of type var.

var is just an implicitly typed variable — we let the C# compiler determine the actual type for us. Here’s one example:

string[] names = { "Tiana", "Dwayne", "Helena" }; var shortNames = names.Where(n => n.Length < 4);

In this case shortNames is actually of type IEnumerable<string>, but we don’t need to worry ourselves about that as long as we have var!

Instructions

1.

Let’s practice using var with LINQ.

Create a variable of type var named shortHeroes and set it equal to this LINQ query:

from h in heroes where h.Length < 8 select h;
2.

Use a foreach loop to print out each element in shortHeroes.

3.

Create another variable of type var named longHeroes and set it equal to this LINQ query:

heroes.Where(n => n.Length > 8);
4.

Use Count() to print the number of elements in longHeroes.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?