什么是C#Linq相当于VB.Net Linq Group Join和Into关键字?

时间:2014-07-22 18:04:59

标签: c# vb.net linq

我正在尝试用C#重写一个旧的vb.net应用程序。我遇到了我需要转换的linq代码:

 (From wn In dtx.WebQueryGroupNames Group Join _
                    wqg In dtx.WebQueryGroups On wn.QueryGroupNameKey Equals wqg.QueryGroupNameKey _
                    Into Group _
                    From p In Group.DefaultIfEmpty() _
                    Select wn _
                    Where Not (From x In dtx.WebQueryGroups Where x.QueryKey = queryKey Select x.QueryGroupNameKey).Contains(wn.QueryGroupNameKey)).Distinct.ToList

到目前为止我所拥有的是:

 (from wn in dtx.WebQueryGroupNames Group join  
        wqg in dtx.WebQueryGroups on wn.QueryGroupNameKey equals wqg.QueryGroupNameKey 
        Into Group 
        from p in Group.DefaultIfEmpty() 
        select wn 
        where !(from x in dtx.WebQueryGroups where x.QueryKey = queryKey select x.QueryGroupNameKey).Contains(wn.QueryGroupNameKey)).Distinct(); 

我似乎无法找到有关Group和Into关键字的任何信息,因为它转换为C#。我想我错过了一个关键点,使这个查询有意义。

2 个答案:

答案 0 :(得分:2)

我相信Group Join会转换为带有join子句的计划into。试试这个

 (from wn in dtx.WebQueryGroupNames join  
        wqg in dtx.WebQueryGroups on wn.QueryGroupNameKey equals wqg.QueryGroupNameKey 
        into g
        from p in g.DefaultIfEmpty() 
        select wn 
        where !(from x in dtx.WebQueryGroups where x.QueryKey = queryKey select x.QueryGroupNameKey).Contains(wn.QueryGroupNameKey)).Distinct();

请参阅http://msdn.microsoft.com/en-us/library/bb397905.aspx

答案 1 :(得分:1)

您正在寻找的术语是Group By

var queryLastNames =
    from student in students
    group student by student.LastName into newGroup
    orderby newGroup.Key
    select newGroup;