【发布时间】:2017-02-10 18:03:33
【问题描述】:
我想将 LINQ 查询的结果直接存储到我的模型中,但我只能通过首先将其存储到变量中来使其工作。
var mdl = (from age in dy.vwFacetPool
group age by new { age.AgeGroup, age.AgeString }
into grp
select new { a = grp.Key.AgeGroup, b = grp.Key.AgeString, c = grp.Count() }).ToList();
但我想一开始就将其存储到模型而不是变量中。
我有模型
public class AgeGroupCounts
{
public int AgeGroup { get; set; }
public string AgeValue { get; set; }
public int AgeCount { get; set; }
}
然后
public class NewEmail
{
public NewEmail()
{
this.Newz = new News();
//this.Age = new AgeGroupCounts();
this.AgeGroup = new List<AgeGroupCounts>();
}
public News Newz { get; set; }
//public AgeGroupCounts Age { get; set; }
public List<AgeGroupCounts> AgeGroup { get; set; }
}
那么我想将 3 个输出字段从选择映射到模型字段,但此时我迷路了
model.AgeGroup = (from age in dy.vwFacetPool
group age by new { age.AgeGroup, age.AgeString }
into grp
select new { grp.Key.AgeGroup, grp.Key.AgeString, grp.Count() }).ToList();
我知道我可以将它存储到 var 和模型中,但这似乎我做错了!
【问题讨论】:
-
将select语句改为:select new AgeGroupCounts { AgeGroup = grp.Key.AgeGroup, AgeValue = grp.Key.AgeString, AgeCount = grp.Count() ?