LINQ_Query_Syntax
LINQ_Query_Syntax
Sample to Practice
Selection
//selects all data objects from the strongly typed
//collection persons. The result of the query If the collection is not strongly typed such an
// is of type IEnumerable<Person> ArrayList, then you need to specify the type of
var r1 = from p in persons select p; the iterator variable.
Ordering
//ordering by age
r1 = from p in persons orderby p.Age select p;
Grouping
//grouping according to age buckets
//The result of the query is of type
//IEnumerable<IGrouping<string, Person>>
var r3 = from p in persons
group p by p.IsFemale;
To Demonstrate
Write the following queries using query syntax. You must also write the appropriate loops to display
results correctly. You will use the same class and data source definitions as you did in the lab. You will
query only the person data store.
Each query must be documented VERY clearly. i.e. copy the problem statement above each of your
query.
The output for each query MUST be simple enough so that grading your work is not too much difficulty,
i.e. separate each output by blank lines and the description of each query.
Code Appendices
class PetOwner
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Pet
{
public string Name { get; set; }
public PetOwner Owner { get; set; }
}