Dynamic LINQ GroupBy Multiple Columns

Ich muss die folgende LINQ-Abfrage in Dynamic LINQ übersetzen, das mehrere Gruppierungsspalten basierend auf Benutzereingaben akzeptiert. Grundsätzlich habe ich eine Reihe von Dropdown-Listen, die Gruppierungen anwenden, und ich möchte nicht jede Kombination von Gruppierungen aufzählen. Wenn Dynamic LINQ fehlschlägt, muss ich möglicherweise manuell eine SQL-Abfrage erstellen, und das möchte niemand.

var grouping = ( from entry in ObjectContext.OmniturePageModules
    where entry.StartOfWeek >= startDate && entry.StartOfWeek <= endDate &&
        ( section == "Total" || section == "All" || entry.Section == section ) &&
        ( page == "Total" || page == "All" || entry.Page == page ) &&
        ( module == "Total" || module == "All" || entry.Module == module ) 
    group entry by new
    {
        entry.Page, // I want to be able to tell this anonymous type
        entry.Module, // which columns to group by
        entry.StartOfWeek // at runtime
    }
    into entryGroup
    select new
    {
        SeriesName = section + ":" + entryGroup.Key.Page + ":" + entryGroup.Key.Module,
        Week = entryGroup.Key.StartOfWeek,
        Clicks = entryGroup.Sum( p => p.Clicks )
    } );

Ich habe keine Ahnung, wie das geht, da Dynamic LINQ außerhalb der "Hallo Welt!" wähle / wo / bestelle nach Fällen. Ich kann die Syntax einfach nicht herausfinden.

Etwas wie:(?

var grouping = ObjectContext.OmniturePageModules.Where(entry => entry.StartOfWeek >= startDate && entry.StartOfWeek <= endDate &&
                                           ( section == "Total" || section == "All" || entry.Section == section ) &&
                                           ( page == "Total" || page == "All" || entry.Page == page ) &&
                                           ( module == "Total" || module == "All" || entry.Module == module ))
                                           .GroupBy("new (StartOfWeek,Page,Module)", "it")
                                           .Select("new (Sum(Clicks) as Clicks, SeriesName = section + key.Page + Key.Module, Week = it.Key.StartOfWeek)");

Ich verwende die DynamicQueryable-Klasse in System.Linq.Dynamic. Sehen:http: //weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1- using-the-linq-dynamic-query-library.asp

Follow-up: Die Lösung von Enigmativity hat funktioniertmeisten. Aus irgendeinem Grund möchte es nicht nach Datum und Uhrzeit gruppieren ("StartOfWeek" -Spalte). Die Problemumgehung besteht nur darin, eine sekundäre Gruppierung durchzuführen:

var entries = ( from entry in ObjectContext.OmniturePageModules
                            where entry.StartOfWeek >= startDate
                                && entry.StartOfWeek <= endDate
                                && ( section == "Total" || section == "All" || entry.Section == section )
                                && ( page == "Total" || page == "All" || entry.Page == page )
                                && ( module == "Total" || module == "All" || entry.Module == module )
                            select entry ).ToArray(); // Force query execution

            var grouping = from entry in entries
                            let grouper = new EntryGrouper( entry, section, page, module )
                            group entry by grouper into entryGroup
                            select new
                            {
                                entryGroup.Key.SeriesName,
                                entryGroup.Key.Date, 
                                Clicks = entryGroup.Sum( p => p.Clicks ),
                            };

            var grouping2 = (from groups in grouping
                            group groups by new {groups.SeriesName, groups.Date } into entryGroup
                            select new
                            {
                               entryGroup.Key.SeriesName,
                               entryGroup.Key.Date,
                               Clicks = entryGroup.Sum( p => p.Clicks ),
                            } );

aber dies scheint die Leistung ernsthaft zu beeinträchtigen ... = /

Antworten auf die Frage(6)

Ihre Antwort auf die Frage