public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public string Category { get; set; }
    public decimal UnitPrice { get; set; }
    public int UnitsInStock { get; set; }
}
    
public class Order
{
    public int OrderID { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal Total { get; set; }
}

public class Customer
{
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public Order[] Orders { get; set; }
}
        
public class Supplier
{
    public string SupplierName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}
        
public class CaseInsensitiveComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
    }
}
        
private class AnagramEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        return getCanonicalString(x) == getCanonicalString(y);
    }

    public int GetHashCode(string obj)
    {
        return getCanonicalString(obj).GetHashCode();
    }

    private string getCanonicalString(string word)
    {
        char[] wordChars = word.ToCharArray();
        Array.Sort<char>(wordChars);
        return new string(wordChars);
    }
}
        
owenG
home learn tableau about
divider
LINQ quiz








divider element

101 LINQ Quiz Overview

Many developers who have been interested in learning details on the syntax and structure of LINQ queries have reviewed Microsoft's 101 LINQ Samples, or played with the related LINQ Sample Queries Project. Both can be very helpful when tackling a specific problem but any knowledge imparted can be transitory and fail to stick in a way that lasts beyond a few hours. One key to persisted learning involves actively trying to remember information through a process known as retrieval practice. The general idea is that the very act of executing a pull request from your mental source code repository reinforces the pathways to where the information is stored and eases future retrievals, i.e. makes you more smart. Other methods of studying, such as reading the LINQ Samples over and over, are often inferior. Those practices may feel more effective than retrieval practice but only because they are easier to perform.

Each of the 15 LINQ quize pages on this site, generally divided by the original LINQ 101 categories, contain the original Microsoft samples but only the "Description" is initially presented. (Click on one of the Operator Categories to the right to see the keywords covered by that page.) Of course it would then be easy enough to click "Show Answer" immediately but the whole idea is to try to come up with the LINQ syntax that would output a sequence (or number, or bool, etc.) matching the description. There is no fancy online code execution or anything else that will guarantee your code is correct, the only "test" is to review your guess after the fact and compare to the provided answers. The closest real-world check would involve copy and paste of your answer to something like LINQPad, a process described over here. Obviously the supplied answers only present one path to the desired output, but they can certainly help get things nailed down if you are stuck over a certain syntax.

One key difference vs. the original Microsoft code is that almost all have been supplemented with alternate answers, so that each sample has answers in both Query and Method (aka Lambda) syntax, where Microsoft had only provided one or the other. Many of the LINQ keywords are only really available with the Method syntax and the related Query answers are often a hybrid of both Query and Method syntaxes. Having both types of answers can also be helpful in order to see scenarios where one or the other syntaxes is clearly more elegant and simple. In relation to Method-only operators, some of the Query answers can be considered to only nominally be in Query syntax, e.g. the initial query is wrapped in parentheses and Method syntax is called on the resulting construct. Those examples often point to Method syntax ease of use. On the other hand, the Join Operators section at the end illustrates several cases where Query syntax is clearly more concise and readable.

Further notes:

  • Samples with operators that are only available with Method syntax will be tagged with "*<LINQ_operator> not directly available with query syntax."
  • The "question" text is taken from the original Microsoft code, with minor additions to clarify the expected result, most notably when the output involves a sequence of anonymous types.
  • The categories for Custom Sequence Operators and Query Execution didn't fit the Q & A format and have been skipped in their entirety.
  • Two bonus questions were added to Generation Operators so that the roundabout carousel had something to go roundabout with.
  • Class definitions for the questions using Products/Customers/Orders/Suppliers will be displayed to the upper-right where samples involve those objects. The code available in Linq data for custom objects represents a cut-down version of the full dataset supplied by Microsoft.
  • Roundabout code for the javascript carousel courtesy of fredleblanc/roundabout