0
编辑3:改进的问题措辞和实施例方法,其接收LINQ IGrouping作为参数
我有一个使用分组下面LINQ查询。分组和选择操作很复杂,所以我将其中一个选择抽象为一个方法,该方法对如何呈现数据做出了一些选择。
我的查询在匿名组定义中正常工作,但只要我将它键入到类中以便将其作为IGrouping对象传递给方法,就会停止对结果进行分组。
public class TestController : Controller
{
public JsonResult ThisWorks()
{
var valueList = DataMocker.GetTestValues();
var group = from v in valueList.AsEnumerable()
where (v.Data != 0)
group v by new
{
Year = v.Fecha.Value.Year,
Trimester = string.Empty,
Month = v.Fecha.Value.Month,
Day = 0,
}
into g
select new SeriesDataPoint
{
y = g.OrderByDescending(obd => obd.Fecha)
.Select(obd => obd.Data.Value)
.FirstOrDefault(),
color = "black",
month = g.Key.Month,
year = g.Key.Year,
seriesName = "Test Series",
};
return Json(group, JsonRequestBehavior.AllowGet);
}
public JsonResult ThisDoesnt()
{
var valueList = DataMocker.GetTestValues();
var group = from v in valueList.AsEnumerable()
where (v.Data != 0)
group v by new Models.SeriesResultGroup
{
Year = v.Fecha.Value.Year,
Trimester = string.Empty,
Month = v.Fecha.Value.Month,
Day = 0,
}
into g
select new SeriesDataPoint
{
y = RenderDataPoint(valueList, g),
color = "black",
month = g.Key.Month,
year = g.Key.Year,
seriesName = "Test Series",
};
return Json(group, JsonRequestBehavior.AllowGet);
}
public static decimal? RenderDataPoint(List<Models.ValoresResultSet> valores, IGrouping<Models.SeriesResultGroup, Models.ValoresResultSet> group)
{
return group.OrderByDescending(obd => obd.Fecha)
.Select(obd => obd.Data.Value)
.FirstOrDefault();
}
}
- 这是正确的输出:https://dl.dropbox.com/u/9764/Thisworks.txt
- 这是一个错误的输出:https://dl.dropbox.com/u/9764/ThisDoesnt.txt
“不工作”不是错误描述。请解释发生了什么。如果您收到错误消息,请将其发布。如果你没有得到想要的结果,请发布预期结果和实际结果。 – 2013-02-19 21:10:19
您在此问题中的代码与您链接到的文件中的代码不匹配。 – juharr 2013-02-19 21:17:23
@DanielHilgarth对不起,不够明确。代码编译并运行,但分组无法按预期工作。事实上,它根本不起作用,我得到所有的行 – amhed 2013-02-19 21:19:35