I want to dynamically add .Select statements, but the compiler will only allow me to build these statements inside the current scope. I am aware of linqkit, which could be a solution, but i dont want to add any external functionallity. How do i avoid this code duplication which i have now:
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
}