Devo tradurre la seguente query LINQ in Dynamic LINQ che accetta diverse colonne di raggruppamento in base all'input dell'utente. Fondamentalmente ho un gruppo di elenchi a discesa che applicano i raggruppamenti e non desidero enumerare ogni combinazione di raggruppamenti. Se Dynamic LINQ fallisce, potrei dover costruire manualmente una query SQL, e nessuno lo vuole.Dynamic LINQ GroupBy Multiple Columns
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)
});
non ho idea di come fare questo, come dinamica LINQ è totalmente documentato al di fuori del "Ciao mondo!" selezionare/where/orderby cases. Non riesco a capire la sintassi.
Qualcosa di simile :(?)
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)");
Sto utilizzando la classe DynamicQueryable in System.Linq.Dynamic. Vedere: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Follow-up: la soluzione di Enigmativity ha lavorato per lo più . Per qualche motivo non vuole gruppo da parte del datetime "StartOfWeek" colonna - soluzione è solo per fare un raggruppamento secondario:
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),
});
ma questo sembra a degradare seriamente le prestazioni ... =/
Grazie mille per la tua ampia risposta. Ci proverò domani e ti faccio sapere se funziona per me - una rapida occhiata è incoraggiante. –
Questo non sembra raggruppare per StartOfWeek per qualche motivo.Ho dovuto modificare il codice di raggruppamento per ogni colonna su if (_section == "All") return _entry.Section; return _section; –
@ 'Daniel Coffman' - Non so perché non è stato raggruppato da' StartOfWeek', dovrebbe avere. Ho ricontrollato il codice e i metodi 'Equals' e' GetHashCode' usano il valore 'StartOfWeek'. Dammi un grido se vuoi che guardi oltre. Mi aspettavo che il codice di raggruppamento per ogni colonna avrebbe probabilmente dovuto essere "ottimizzato" un po 'per le tue esigenze. – Enigmativity