简体   繁体   English

如何在Linq查询中通过group by语句使用我的类属性?

[英]How can I use my class properties in my Linq query with group by statement?

I would like to use my entity class properties in my linq query that return some value with it. 我想在我的linq查询中使用实体类属性,该属性随其返回一些值。

so this my linq query; 所以这是我的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();

so for instance I would like to set Name properties with my entity framework model class' properties' value, Name = s.Name, 因此,例如,我想使用实体框架模型类的“属性”值Name = s.Name来设置Name属性,

How can I implement this on my code? 如何在我的代码上实现呢?

您可以通过在g上执行另一个linq查询来访问s.Name的方法,因为g现在是分组对象的数据集。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM