各位,请教如何写此类的函数??
代码:
var datas = (from D in dbjy.dt_2_jiens
where (D.单位名称 == "办公室")
group D by D.类别 into dGroup
select new {tts=dGroup.Key,conts=dGroup.Count()}).OrderBy(D=>D.tts ) ;
// 开始
List<SelectListItem> xx = new List<SelectListItem>();
if ((datas != null) && (datas.Count() > 0))
{
foreach (var data in datas)
{
xx.Add(new SelectListItem { Text = tts , Value = conts.ToString() });
}
}
// 结束
ViewData["Chart"] = xx ;
return View();
var datas = (from D in dbjy.dt_2_jiens
where (D.单位名称 == "办公室")
group D by D.类别 into dGroup
select new {tts=dGroup.Key,conts=dGroup.Count()}).OrderBy(D=>D.tts ) ;
ViewData["Chart"] = getdata(datas) ;
return View();
[解决办法]
嗯,Linq to EF不支持ToString()
只能这么写了:
List<SelectListItem>?getData<T>(IEnumerable<T>?data,?Func<T,?SelectListItem>?selector)?where?T?: class
{
????return?data.Select(selector).ToList();
}
ViewData["Chart"] = getData(datas.AsEnumerable(), x => new SelectListItem { Text = tts, Value = conts.ToString() });