SQL转换为LINQ Group by convertion

时间:2012-09-28 02:22:59

标签: sql linq

我有一个SQL语句,我想将其转换为LINQ。现在的问题是我不知道如何在LINQ中对它进行分组。

这是代码。

在SQL中

select plp.ProspectsListID, p.Prospect_PII_Key 
from ProspectListProspect plp
join Prospects p
on p.ProspectsID = plp.ProspectsID
group by plp.ProspectsListID,p.Prospect_PII_Key

在LINQ中

  var list1 = from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
                    join p in     GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
                        on plp.ProspectsID equals p.ProspectsID
                    select new
                    {
                        ProspectID = plp.ProspectsListID,
                        Prospect_PII_Key = p.Prospect_PII_Key
                    };

谢谢

杰森

3 个答案:

答案 0 :(得分:2)

tyr this

var list1 = from item in
                (
                   from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
                join p in     GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
                    on plp.ProspectsID equals p.ProspectsID
                  select new
                  {
                      ProspectID = plp.ProspectsListID,
                      Prospect_PII_Key = p.Prospect_PII_Key
                   }
                )
                group item by new {item.ProspectID ,item.Prospect_PII_Key } into grp
                select new
                {
                    ProspectID = grp.ProspectsListID,
                    Prospect_PII_Key = grp.Prospect_PII_Key
                }

答案 1 :(得分:1)

检查

var list1 = from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
                    join p in     GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
                        on plp.ProspectsID equals p.ProspectsID
             Group By Key = New With {plp.ProspectsListID,p.Prospect_PII_Key} Into Group                       
             Select Group;

答案 2 :(得分:1)

var list1 = from plp in GetDataContext.SQLDataContext.GetTable<DataAccess.ProspectListProspect>()
            join p in  GetDataContext.SQLDataContext.GetTable<DataAccess.Prospect>()
                   on plp.ProspectsID equals p.ProspectsID
            group p by new {plp.ProspectsListID,p.Prospect_PII_Key} into g
            select new
                {
                    ProspectID = g.Key.ProspectsListID,
                    Prospect_PII_Key = g.Key.Prospect_PII_Key
                };