Essential C# LINQ Queries  Suggest an edit

Joining on multiple keys

PropertyInfo[] stringProps = typeof (string).GetProperties(); // string properties
PropertyInfo[] builderProps = typeof(StringBuilder).GetProperties(); // stringbuilder properties

var query =
    from s in stringProps
    join b in builderProps
        on new { s.Name, s.PropertyType } equals new { b.Name, b.PropertyType }
    select new
    {
        s.Name,
        s.PropertyType,
        StringToken = s.MetadataToken,
        StringBuilderToken = b.MetadataToken
    };

Note that anonymous types in above join must contain same properties since objects are considered equal only if all their properties are equal. Otherwise query won’t compile.


Table Of Contents
22 Enum
25 GUID
65 LINQ Queries
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close