而不是FirstOrDefault()我用什么来获取分组中的所有值?

时间:2015-11-04 11:52:02

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

如何获取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();
}

Here  attaching the image of table values :

enter image description here

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

1 个答案:

答案 0 :(得分:3)

您可以使用Select进行投影,如下所示: -

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