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

LINQPad

If you are interested in learning LINQ and/or refreshing your knowledge, installation of the free LINQPad is highly recommended. An apt description comes from the LINQ Secrets Revealed: Chaining and Debugging article on Simple Talk:

LINQPad is one of those rare applications that is elegant, powerful, and well-designed. As soon as you start using it you know it is “just right”. Remember back when you discovered the awesome power of Visual Studio; LINQPad is like that, too. ... LINQPad is a sandbox/IDE for .NET languages (C#, VB, SQL, F#) that lets you develop code without all the overhead of creating solutions and projects required in Visual Studio.

There is no real integration of the 101 LINQ samples on this site and LINQPad but for the ones that work with self-contained collections, i.e. do not use Products/Customers/Orders/Suppliers classes, testing an answer requires little more than a copy & paste:

  1. Open LINQPad and if necessary set the Language dropdown to "C# Statement(s)"
  2. Copy & paste the collection definition
  3. Enter the code necessary to answer question directly into the Query box, or copy from roundabout
  4. Append ".Dump()" to the output variable
  5. Press F5 or the green Execute arrow
LINQPad illustration

For the many samples referencing the Products/Customers/Orders/Suppliers the steps will be somewhat more complicated, at least the first time:

  1. Open LINQPad and in a new Query tab set the Language dropdown to "C# Program"
  2. Copy & paste the collection definition and your answer code into the provided Main method
  3. Append ".Dump()" to the output variable
  4. Underneath closing block of Main() paste the code contents of LINQ custom object data
  5. Press F5 or the green Execute arrow
LINQPad illustration

After confirming successful execution it makes sense to save the Query as a permanent .linq file and then for each future test replace the contents of Main() as appropriate.