【问题标题】:Instead of FirstOrDefault() what do I use to get all values in group by?而不是 FirstOrDefault() 我用什么来获取分组中的所有值?
【发布时间】:2015-11-04 11:52:01
【问题描述】:

如何获取 SpecificationDetails_ID 的所有值。通过下面的代码,我只是从一组值中获取第一个值。如何获取所有值?

using(APM context=new APM())
{
      var  lstprodspc = (from s in context.M_ProductSpecifaction
    //join p in context.M_SpecificationDetails on s.SpecificationDetails_ID equals p.ID
    // join r in context.M_Specifications on p.Specification_ID equals r.ID
                          where s.Product_ID == P_ID
                          group s by s.Parent_ID into pg
                          select new 
                          {
                              ProductSD_ID = pg.FirstOrDefault().SpecificationDetails_ID 
                          }).ToList();
}

从上面的代码我只得到 34,31,31,31,26,26,26,26。

【问题讨论】:

    标签: c# entity-framework linq asp.net-mvc-4


    【解决方案1】:

    您可以像这样使用Select 进行投影:-

     select new 
            {
               Parent_ID = pg.Key,
               ProductSD_ID = pg.Select(x => x.SpecificationDetails_ID).ToList()
            }).ToList();
    

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多