C# 我应该使用什么来获取group by中的所有值,而不是FirstOrDefault()?
如何获取SpecificationDetails\u ID的所有值。通过下面的代码,我只需从一组值中获取第一个值。如何获取所有值C# 我应该使用什么来获取group by中的所有值,而不是FirstOrDefault()?,c#,entity-framework,linq,asp.net-mvc-4,C#,Entity Framework,Linq,Asp.net Mvc 4,如何获取SpecificationDetails\u 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
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,26,26,26,26。您可以使用
选择
如下方式进行投影:-
select new
{
Parent_ID = pg.Key,
ProductSD_ID = pg.Select(x => x.SpecificationDetails_ID).ToList()
}).ToList();
您可以使用
选择进行投影,如下所示:-
select new
{
Parent_ID = pg.Key,
ProductSD_ID = pg.Select(x => x.SpecificationDetails_ID).ToList()
}).ToList();