按ID分组记录

时间:2014-12-02 11:13:35

标签: linq entity-framework linq-to-entities

我从存储过程返回这样的记录:

Id       FullName         Review      Rating
---------------------------------------------
1         john              sk         4.5
1         john              hhh        3.5
1         john              jhj        1.5
2         rig               www        3.5
2         rig               eee        1.5

这是我的查询返回上面的记录:

var empDetails = context.Database.SqlQuery<SearchWorkerDetail>("exec SearchWorkerDetail @param1", new SqlParameter("param1", searchKeyword) ) 
                       .Select( d => new
                                     {
                                        id = d.Id,
                                        FullName = d.FullName,
                                        Email = d.Email,
                                        ServiceDescription = d.ServiceDescription,
                                        Skills = d.Skills,
                                        Name = d.Name,
                                        r = d.Review,
                                        averagerating = d.Rating
                                     }).ToList();

现在我想按Id对记录进行分组,并希望选择数据。

预期产出:

SearchWorkerDetail=
    [0]:{
    Id:1
    FullName:John
    Email:john@yahoo.com
      ReviewList:
      {
         [0]: Review=sk
              rating=4.5
         [1]: Review=hhh
              rating=3.5
         [2]: Review=jhj
              rating=1.5
      }
    [1]:{
    Id:2
    FullName:rig
    Email:rig@yahoo.com
      ReviewList:
      {
         [0]: Review=www
              rating=3.5
         [1]: Review=eee
              rating=1.5
      }
    }

对于相应的ID我想要评论列表(工人和评论之间的1对多关系)

我的班级结构:

[DataContract]
public class SearchWorkerDetail
{
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string FullName { get; set; }
        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string ServiceDescription { get; set; }
        [DataMember]
        public string Skills { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Review { get; set; }
        [DataMember]
        public Nullable<decimal> Rating { get; set; }

        [DataMember]
        public List<ReviewModel> RatingList { get; set; }
}

我的查询是这样的:

var data = empDetails.GroupBy(m=>m.id)
                     .Select(g => new SearchWorkerDetail
                                       {
                                          id  =g.Key,
                                          FullName = g.FullName,
                                          Reviewlist =

                                        }
                                 ).ToList();

这里不允许我选择FullName和其他(例如:电子邮件,技能描述,技能等)

我该怎么做?

1 个答案:

答案 0 :(得分:1)

FullName上没有g属性,因为它是而不是单个对象。但是,您可以获取该组中第一条记录的FullName,然后使用另一条ReviewList为该组中的每条记录创建Select

empDetails.GroupBy(m => m.id)
    .Select(g => new SearchWorkerDetail
                 {
                     id = g.Key,
                     FullName = g.First().FullName,
                     Reviewlist= g.Select(x => new ReviewModel
                                               { 
                                                   Review = x.Review, 
                                                   rating = x.Rating
                                               }.ToList()
                 }).ToList();

或者您也可以根据FullName分组:

empDetails.GroupBy(m => new { m.id, m.FullName })
    .Select(g => new SearchWorkerDetail
                 {
                     id = g.Key.id,
                     FullName = g.Key.FullName,    
                     /* rest is the same */
                 }).ToList();