LINQ增长图表

时间:2011-09-18 05:20:52

标签: c# linq

我生病了这个问题。我有一些表在特定日期向某个地区发起了竞争。 我正在创建一个MVC应用程序,它将显示按日期增长的图表。

我正在尝试制作一个接收int天数的方法,并计算每天发起的内容量直到当天。例如:

地区en-US

  • 内容X - 日期12/03/2010
  • 内容Y - 日期12/03/2010
  • 内容Z - 日期13/03/2010

地区pt-BR

  • 内容A - 日期13/03/2010
  • 内容B - 日期14/03/2010

地区ja-JP

  • 内容J - 日期15/03/2010

因此图表必须显示:

日期12/03/2010:

  • en-US:2
  • pt-BR:0
  • ja-JP:0

日期13/03/2010

  • en-US:1
  • pt-BR:1
  • ja-JP:0

日期14/03/2010

  • en-US:0
  • pt-BR:1
  • ja-JP:0

日期15/03/2010

  • en-US:0
  • pt-BR:0
  • ja-JP:1

尝试进行此查询时感到无能为力。

任何人都可以提供帮助吗?

这里,我创建的其他方法是显示每个国家/地区的日常数字:

public KeyValuePair<string, List<KeyValuePair<string, int>>>[] ChartDayByDay(int days, string locale)
    {
        KeyValuePair<string, List<KeyValuePair<string, int>>>[] contagens;

        DateTime inicio = DateTime.Today.AddDays(days * (-1));

        var qry = from c in db.XBLRegionalInfos
                  where c.PublishDate != null
                  && c.PublishDate > inicio
                  select c;

        if (!String.IsNullOrEmpty(locale))
            qry = qry.Where(x => x.RegionId == locale);

        var qry2 = (from c in qry
                    group c by c.RegionId into g
                    let count = g.Count()
                    where count > 0
                    select g).ToList();

        var regioes = db.XBLRegions.ToList();

        contagens = new KeyValuePair<string, List<KeyValuePair<string, int>>>[qry2.Count];

        for (int i = 0; i < qry2.Count; i++)
        {
            for (int j = 0; j < qry2[i].Count(); j++)
            {
                var pais = qry2[i].ElementAt(j).Region.Country;

                contagens[i] = new KeyValuePair<string, List<KeyValuePair<string, int>>>(
                    pais, CountPeriod(days, qry2[i].ElementAt(j).RegionId));
            }
        }

        return contagens;
    }

1 个答案:

答案 0 :(得分:2)

这至少与linq一起使用对象:

public KeyValuePair<string, List<KeyValuePair<string, int>>>[] ChartDayByDay(int days, string locale)
{
    return db
        .XBLRegionalInfos
        .Where(x => string.IsNullOrEmpty(locale) || x.RegionId == locale)
        .Where(x => x.PublishDate > DateTime.Today.AddDays(days * (-1)))
        .GroupBy(x => new {x.PublishDate.Date, x.RegionId})
        .Select(x => new {x.Key, Count = x.Count()})
        .GroupBy(x => x.Key.Date)
        .Select(x =>
                new KeyValuePair<string, List<KeyValuePair<string, int>>>(
                    x.Key.ToString(),
                    x.Select(y => new KeyValuePair<string, int>(y.Key.RegionId, y.Count)).ToList()))
        .ToArray();
}