【问题标题】:How can I use my class properties in my Linq query with group by statement?如何通过 group by 语句在 Linq 查询中使用我的类属性?
【发布时间】:2015-03-31 16:03:27
【问题描述】:

我想在我的 linq 查询中使用我的实体类属性来返回一些值。

所以这是我的 linq 查询;

List<PvmBarChartData> BaseofSegmentIPPGMR = (from si in db.ScoreItem
 join s in db.Score on si.ScoreId equals s.Id
 join prg in db.ProjectResearchGroup on si.ProjectResearchGroupId equals prg.Id
 join rg in db.RgClone on prg.RgCloneId equals rg.Id
 join sp in db.SalesPoint on s.SalesPointId equals sp.Id
 join c in db.Channel on sp.ChannelId equals c.Id
 where (si.ResearchGroupType == ResearchGroupType.ScoreCard && spIds.Contains(s.SalesPointId))
 group si by c.Name into g
 select new PvmBarChartData
 {
    GroupName = g.Key,

    DataValues = new List<CvmNameValuePair>{ new CvmNameValuePair{

    Name = "",
    Value = g.Average(x => x.TotalScore)
 }
}
})
.ToList();

例如,我想用我的实体框架模型类的“属性”值设置名称属性,名称 = s.Name,

如何在我的代码中实现这一点?

【问题讨论】:

    标签: c# linq entity-framework


    【解决方案1】:

    访问 s.Name 的一种方法是对 g 执行另一个 linq 查询,因为 g 现在是分组对象的数据集。

    Name = (from gx in g select gx.Name).FirstorDefault();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2015-02-16
      • 1970-01-01
      相关资源
      最近更新 更多