Grupo LINQ dinâmicoPor várias colunas
Preciso converter a seguinte consulta LINQ no Dynamic LINQ que aceita várias colunas de agrupamento com base na entrada do usuário. Basicamente, tenho várias listas suspensas que aplicam agrupamentos e não quero enumerar todas as combinações de agrupamentos. Se o Dynamic LINQ falhar, talvez seja necessário criar uma consulta SQL manualmente e ninguém deseja isso.
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 )
} );
Não tenho idéia de como fazer isso, pois o Dynamic LINQ é totalmente indocumentado fora do "olá mundo!" selecione / onde / encomendarpor casos. Eu simplesmente não consigo descobrir a sintaxe.
Algo como:(?)
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)");
Estou usando a classe DynamicQueryable em System.Linq.Dynamic. Vejo:http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Acompanhamento: a solução da Enigmativity funcionouna maioria das vezes. Por alguma razão, ele não deseja agrupar pela coluna "StartOfWeek" na data e hora - a solução alternativa é apenas para fazer um agrupamento secundário:
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 ),
} );
mas isso parece prejudicar seriamente o desempenho ... = /