Bird
Raised Fist0

You want to project a list of anonymous objects with Id and NameLength for the length of Name from a list of Person. Which LINQ query achieves this?

hard🚀 Application Q9 of Q15
C Sharp (C#) - LINQ Fundamentals
You want to project a list of anonymous objects with Id and NameLength for the length of Name from a list of Person. Which LINQ query achieves this?
Avar result = from p in people select new { p.Id, NameLength = p.Name.Length };
Bvar result = from p in people select new { p.Id, p.Name.Length };
Cvar result = from p in people select new { Id = p.Id, Length(p.Name) };
Dvar result = from p in people select new { p.Id, Length = p.Name.Length() };
Step-by-Step Solution
Solution:
  1. Step 1: Understand anonymous object syntax

    Anonymous objects can have named properties with expressions, e.g., NameLength = p.Name.Length.
  2. Step 2: Check each option

    var result = from p in people select new { p.Id, NameLength = p.Name.Length }; correctly names the length property. var result = from p in people select new { p.Id, p.Name.Length }; misses property name, C uses invalid syntax, D incorrectly calls Length as method.
  3. Final Answer:

    var result = from p in people select new { p.Id, NameLength = p.Name.Length }; -> Option A
  4. Quick Check:

    Anonymous object with named properties [OK]
Quick Trick: Name properties explicitly in anonymous objects [OK]
Common Mistakes:
MISTAKES
  • Omitting property names in anonymous types
  • Using method syntax incorrectly
  • Confusing property access with method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes