我想动态添加 .Select 语句,但编译器只允许我在当前范围内构建这些语句。我知道 linqkit,它可能是一个解决方案,但我不想添加任何外部功能。我如何避免我现在拥有的这种代码重复:
if (request.Equals("some text"))
{
var filter = commonFilter.GroupBy(x => new
{
x.Timestamp.Year,
x.Timestamp.Month,
})
.Select(g => new
{
Year = g.Key.Year,
Month = g.Key.Month,
TotErr = g.Count(),
column1 = g.Sum(o => o.Process.Contains("something") ? 1 : 0),
column2= g.Sum(o => o.Process.Contains(".something1") ? 1 : 0),
column3= g.Sum(o => o.Process.Contains(".something2") ? 1 : 0),
column4= g.Sum(o => o.Process.Contains("something3") ? 1 : 0),
column5= g.Sum(o => o.Process.Contains(".something4") ? 1 : 0),
column6 = g.Sum(o => o.Process.Contains(".something5") ? 1 : 0),
column7= g.Sum(o => o.Process.Contains(".something6") ? 1 : 0),
column8= g.Sum(o => o.Process.Contains(".something7") ? 1 : 0),
column9= g.Sum(o => o.Process.Contains(".something8") ? 1 : 0),
NumOrgs = g.Select(l => l.OrganizationId).Distinct().Count(),
NumUsers = g.Select(l => l.UserId).Distinct().Count(),
});
return filter.AsPa......;//not important right now
}
//Same thing again but slightly different
else if (request.Equals("some other text"))
{
var filter = commonFilter.GroupBy(x => new
{
x.Timestamp.Year,
x.Timestamp.Month,
x.Timestamp.Day
})
.Select(g => new
{
Year = g.Key.Year,
Month = g.Key.Month,
Day= g.Key.Day,
TotErr = g.Count(),
column1 = g.Sum(o => o.Process.Contains("something") ? 1 : 0),
column2= g.Sum(o => o.Process.Contains(".something1") ? 1 : 0),
column3= g.Sum(o => o.Process.Contains(".something2") ? 1 : 0),
column4= g.Sum(o => o.Process.Contains("something3") ? 1 : 0),
column5= g.Sum(o => o.Process.Contains(".something4") ? 1 : 0),
column6 = g.Sum(o => o.Process.Contains(".something5") ? 1 : 0),
column7= g.Sum(o => o.Process.Contains(".something6") ? 1 : 0),
column8= g.Sum(o => o.Process.Contains(".something7") ? 1 : 0),
column9= g.Sum(o => o.Process.Contains(".something8") ? 1 : 0),
NumOrgs = g.Select(l => l.OrganizationId).Distinct().Count(),
NumUsers = g.Select(l => l.UserId).Distinct().Count(),
});
return filter.AsPa......;//not important right now
}