I have a function that looks like this:
public void GetAvailableResourcesByLocationChart(List<DateTime> dates, List<ChartResourceModel> data)
{
var totals = (from r in data
group new { Dates = r.Dates } by r.Location into g
select new
{
Location = g.Key,
Dates = dates.Select(d => new
{
Date = d,
Available = g.SelectMany(x => x.Dates.Where(y => y.Date == d)).Count(x => x.Available)
})
.OrderBy(x => x.Date)
.ToList()
})
.OrderBy(x => x.Location)
.ToList();
}
This example groups the data based on Location. But I want to be able to pass in a string that specifies what it should group on. I was thinking DynamicLinq would be the right way to go about this but I'm having trouble reproducing it.
I started off doing this, but am getting stuck reproducing the SelectMany inside the select:
public void GetAvailableResourcesByLocationChart(List<DateTime> dates, List<ChartResourceModel> data, string grouping)
{
var totals = data.GroupBy(grouping, "it").Select("new (it.Key as Group, it as Dates)").Cast<dynamic>();
}
Any ideas on what I need to do next?
this
from r in data
group new { Dates = r.Dates } by r.Location into g
...
is the same as this
data.GroupBy(r => r.Location, b => b.Dates)
so if we have variable named grouper
data.GroupBy(r => {
if (grouper == "L")
return r.Location
else
return r.Dates }, b => b.Dates);
This should get you on the right track?