Listnames = new List () { "John", "Jane", "Bob", "Alice" }; var result = from name in names where name.StartsWith("J") select name; foreach (string name in result) { Console.WriteLine(name); }
class Person { public string Name { get; set; } public int Age { get; set; } } ListThis code groups a list of Person objects by their Age property. It then prints out each group and the names of the people in that group. Package library: System.Linq Overall, C# Query is a useful library for manipulating and querying data structures in C#. It is part of the System.Linq namespace and provides a convenient way to use LINQ syntax in C#.people = new List () { new Person() { Name = "John", Age = 26 }, new Person() { Name = "Jane", Age = 24 }, new Person() { Name = "Bob", Age = 26 }, new Person() { Name = "Alice", Age = 23 }, }; var result = from person in people group person by person.Age; foreach (var group in result) { Console.WriteLine($"People with age {group.Key}:"); foreach (Person person in group) { Console.WriteLine(person.Name); } }