深入理解 c# 第十一章 使用分组连接连接缺陷和订阅 使用join...into子句进行分组连接

    class AllDefectsToSubscribersGrouped
    {
        static void Main()//使用join...into子句进行分组连接 使用分组连接连接缺陷和订阅
        {
            var query = from defect in SampleData.AllDefects 
                        join subscription in SampleData.AllSubscriptions
                             on defect.Project equals subscription.Project
							 //subscription.Project {Chapter11.Model.NotificationSubscription}
//defect							 
//{ 1: MP3 files crash system    (2013/5/1-2013/5/23, Showstopper/Accepted, Tim Trotter -> Darren Dahlia)}
//defect = { 2: Text is too big(2013/5/3-2013/5/9, Trivial/Closed, Deborah Denton -> n/a)}     
//{ 3: Sky is wrong shade of blue    (2013/5/3-2013/5/19, Minor/Fixed, Colin Carton -> Tara Tutu)}  
//{ 4: Can't play files more than 200 bytes long    (2013/5/4-2013/5/23, Major/Reopened, Darren Dahlia -> Darren Dahlia)}  
//{ 5: Installation is slow    (2013/5/6-2013/5/15, Trivial/Fixed, Tim Trotter -> Tim Trotter)}  
//{ 6: DivX is choppy on Pentium 100    (2013/5/7-2013/5/29, Major/Accepted, Mary Malcop -> Darren Dahlia)}  
//{ 7: Client acts as virus    (2013/5/8-2013/5/10, Showstopper/Closed, Deborah Denton -> n/a)}  
//{ 8: Subtitles only work in Welsh(2013/5/8-2013/5/23, Major/Fixed, Darren Dahlia -> Tim Trotter)}    
//{ 9: Voice recognition is confused by background noise    (2013/5/9-2013/5/15, Minor/Closed, Colin Carton -> n/a)}  
//{10: User interface should be more caramelly    (2013/5/9-2013/5/9, Trivial/Created, Tim Trotter -> Darren Dahlia)}  
//{11: Burning a CD makes the printer catch fire    (2013/5/10-2013/5/29, Showstopper/Closed, Mary Malcop -> n/a)}  
//{12: Peer to peer pairing passes parameters poorly    (2013/5/10-2013/5/12, Minor/Accepted, Tara Tutu -> Darren Dahlia)}  
//{13: Delay when sending message    (2013/5/11-2013/5/20, Minor/Fixed, Darren Dahlia -> Tara Tutu)}  
//{14: Volume control needs to go to 11    (2013/5/11-2013/5/11, Minor/Created, Mary Malcop -> Darren Dahlia)}  
//{15: Splash screen fades too quickly    (2013/5/11-2013/5/15, Minor/Fixed, Colin Carton -> Tara Tutu)}  
//{16: Text box doesn't keep up with fast typing    (2013/5/12-2013/5/12, Major/Accepted, Deborah Denton -> Deborah Denton)}  
//{17: Password displayed in plain text    (2013/5/12-2013/5/13, Showstopper/Closed, Darren Dahlia -> n/a)}  
//{18: Play button points the wrong way    (2013/5/12-2013/5/17, Major/Fixed, Tim Trotter -> Tim Trotter)}  
//{19: Wizard needed for CD burning    (2013/5/13-2013/5/20, Minor/Fixed, Colin Carton -> Colin Carton)}  
//{20: Subtitles don't display during fast forward    (2013/5/13-2013/5/14, Trivial/Accepted, Mary Malcop -> Darren Dahlia)}  
//{21: Memory leak when watching Memento    (2013/5/13-2013/5/13, Trivial/Created, Darren Dahlia -> Deborah Denton)}  
//{22: Profile screen shows login count of -1    (2013/5/13-2013/5/20, Major/Accepted, Deborah Denton -> Deborah Denton)}  
//{23: Server crashes under heavy load (3 users)    (2013/5/13-2013/5/17, Major/Accepted, Tim Trotter -> Deborah Denton)}  
//{24: Unable to connect to any media server    (2013/5/15-2013/5/18, Showstopper/Reopened, Tara Tutu -> Darren Dahlia)}  
//{25: UI turns black and white when playing old films    (2013/5/15-2013/5/25, Minor/Fixed, Deborah Denton -> Tara Tutu)}  
//{26: Password reset changes passwords for all users    (2013/5/16-2013/5/18, Showstopper/Closed, Mary Malcop -> n/a)}  
//{27: Modern music sounds rubbish    (2013/5/17-2013/5/17, Trivial/Created, Tim Trotter -> Darren Dahlia)}  
//{28: Webcam makes me look bald    (2013/5/18-2013/5/27, Showstopper/Fixed, Tim Trotter -> Tim Trotter)}  
//{29: Sound is distorted when speakers are underwater    (2013/5/18-2013/5/18, Major/Created, Colin Carton -> Darren Dahlia)}  
//{30: Japanese characters don't display properly    (2013/5/19-2013/5/23, Major/Accepted, Darren Dahlia -> Deborah Denton)}  
//{31: Video takes 100% of CPU    (2013/5/20-2013/5/22, Major/Accepted, Tara Tutu -> Deborah Denton)}  
//{32: DVD Easter eggs unavailable    (2013/5/22-2013/5/22, Trivial/Created, Tim Trotter -> Darren Dahlia)}  
//{33: Transparency is high for menus to be readable    (2013/5/23-2013/5/25, Minor/Accepted, Mary Malcop -> Deborah Denton)}  
//{34: About box is missing version number    (2013/5/24-2013/5/29, Minor/Fixed, Colin Carton -> Colin Carton)}  
//{35: Logs record confidential conversations    (2013/5/25-2013/5/30, Major/Reopened, Tim Trotter -> Darren Dahlia)}  
//{36: Profanity filter is too aggressive    (2013/5/27-2013/5/29, Minor/Fixed, Deborah Denton -> Tara Tutu)}  
//{37: Full screen mode fails on dual monitors    (2013/5/27-2013/5/27, Minor/Created, Tara Tutu -> Deborah Denton)}  
//{38: Visualization hypnotises pets    (2013/5/28-2013/5/29, Minor/Accepted, Colin Carton -> Deborah Denton)}  
//{39: Resizing while typing loses input    (2013/5/29-2013/5/29, Trivial/Created, Mary Malcop -> Darren Dahlia)}  
//{40: Network is saturated when playing WAV file    (2013/5/30-2013/5/31, Minor/Fixed, Tim Trotter -> Tim Trotter)}  
//{41: Media library tells user to keep the noise down    (2013/5/31-2013/5/31, Major/Created, Tara Tutu -> Darren Dahlia)}  
                             into groupedSubscriptions
                        select new { Defect=defect, Subscriptions=groupedSubscriptions };
//{System.Linq.Enumerable.GroupJoinIterator<Chapter11.Model.Defect,Chapter11.Model.NotificationSubscription,Chapter11.Model.Project,<>f__AnonymousType3<Chapter11.Model.Defect,System.Collections.Generic.IEnumerable<Chapter11.Model.NotificationSubscription>>>}
//groupedSubscriptions {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping}

            foreach (var entry in query)
//entry			
//{ Defect = { 1: MP3 files crash system    (2013/5/1-2013/5/23, Showstopper/Accepted, Tim Trotter -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }			
//{ Defect = { 2: Text is too big    (2013/5/3-2013/5/9, Trivial/Closed, Deborah Denton -> n/a)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 3: Sky is wrong shade of blue    (2013/5/3-2013/5/19, Minor/Fixed, Colin Carton -> Tara Tutu)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 4: Can't play files more than 200 bytes long    (2013/5/4-2013/5/23, Major/Reopened, Darren Dahlia -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 5: Installation is slow    (2013/5/6-2013/5/15, Trivial/Fixed, Tim Trotter -> Tim Trotter)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 6: DivX is choppy on Pentium 100    (2013/5/7-2013/5/29, Major/Accepted, Mary Malcop -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 7: Client acts as virus    (2013/5/8-2013/5/10, Showstopper/Closed, Deborah Denton -> n/a)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 8: Subtitles only work in Welsh    (2013/5/8-2013/5/23, Major/Fixed, Darren Dahlia -> Tim Trotter)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = { 9: Voice recognition is confused by background noise    (2013/5/9-2013/5/15, Minor/Closed, Colin Carton -> n/a)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {10: User interface should be more caramelly    (2013/5/9-2013/5/9, Trivial/Created, Tim Trotter -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {11: Burning a CD makes the printer catch fire    (2013/5/10-2013/5/29, Showstopper/Closed, Mary Malcop -> n/a)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {12: Peer to peer pairing passes parameters poorly    (2013/5/10-2013/5/12, Minor/Accepted, Tara Tutu -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {13: Delay when sending message    (2013/5/11-2013/5/20, Minor/Fixed, Darren Dahlia -> Tara Tutu)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {14: Volume control needs to go to 11    (2013/5/11-2013/5/11, Minor/Created, Mary Malcop -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {15: Splash screen fades too quickly    (2013/5/11-2013/5/15, Minor/Fixed, Colin Carton -> Tara Tutu)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {16: Text box doesn't keep up with fast typing    (2013/5/12-2013/5/12, Major/Accepted, Deborah Denton -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {17: Password displayed in plain text    (2013/5/12-2013/5/13, Showstopper/Closed, Darren Dahlia -> n/a)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {18: Play button points the wrong way    (2013/5/12-2013/5/17, Major/Fixed, Tim Trotter -> Tim Trotter)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {19: Wizard needed for CD burning    (2013/5/13-2013/5/20, Minor/Fixed, Colin Carton -> Colin Carton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {20: Subtitles don't display during fast forward    (2013/5/13-2013/5/14, Trivial/Accepted, Mary Malcop -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {21: Memory leak when watching Memento    (2013/5/13-2013/5/13, Trivial/Created, Darren Dahlia -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {22: Profile screen shows login count of -1    (2013/5/13-2013/5/20, Major/Accepted, Deborah Denton -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {23: Server crashes under heavy load (3 users)    (2013/5/13-2013/5/17, Major/Accepted, Tim Trotter -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {24: Unable to connect to any media server    (2013/5/15-2013/5/18, Showstopper/Reopened, Tara Tutu -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {25: UI turns black and white when playing old films    (2013/5/15-2013/5/25, Minor/Fixed, Deborah Denton -> Tara Tutu)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {26: Password reset changes passwords for all users    (2013/5/16-2013/5/18, Showstopper/Closed, Mary Malcop -> n/a)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {27: Modern music sounds rubbish    (2013/5/17-2013/5/17, Trivial/Created, Tim Trotter -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {28: Webcam makes me look bald    (2013/5/18-2013/5/27, Showstopper/Fixed, Tim Trotter -> Tim Trotter)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {29: Sound is distorted when speakers are underwater    (2013/5/18-2013/5/18, Major/Created, Colin Carton -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {30: Japanese characters don't display properly    (2013/5/19-2013/5/23, Major/Accepted, Darren Dahlia -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {31: Video takes 100% of CPU    (2013/5/20-2013/5/22, Major/Accepted, Tara Tutu -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {32: DVD Easter eggs unavailable    (2013/5/22-2013/5/22, Trivial/Created, Tim Trotter -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {33: Transparency is high for menus to be readable    (2013/5/23-2013/5/25, Minor/Accepted, Mary Malcop -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {34: About box is missing version number    (2013/5/24-2013/5/29, Minor/Fixed, Colin Carton -> Colin Carton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {35: Logs record confidential conversations    (2013/5/25-2013/5/30, Major/Reopened, Tim Trotter -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {36: Profanity filter is too aggressive    (2013/5/27-2013/5/29, Minor/Fixed, Deborah Denton -> Tara Tutu)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {37: Full screen mode fails on dual monitors    (2013/5/27-2013/5/27, Minor/Created, Tara Tutu -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {38: Visualization hypnotises pets    (2013/5/28-2013/5/29, Minor/Accepted, Colin Carton -> Deborah Denton)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {39: Resizing while typing loses input    (2013/5/29-2013/5/29, Trivial/Created, Mary Malcop -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {40: Network is saturated when playing WAV file    (2013/5/30-2013/5/31, Minor/Fixed, Tim Trotter -> Tim Trotter)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }
//{ Defect = {41: Media library tells user to keep the noise down    (2013/5/31-2013/5/31, Major/Created, Tara Tutu -> Darren Dahlia)}, Subscriptions = {System.Linq.Lookup<Chapter11.Model.Project,Chapter11.Model.NotificationSubscription>.Grouping} }

            {
                Console.WriteLine(entry.Defect.Summary);
                foreach (var subscription in entry.Subscriptions)
				//var subscription {Chapter11.Model.NotificationSubscription}
				
                {
                    Console.WriteLine ("  {0}", subscription.EmailAddress);
                }
            }
        }
    }


  分组连接的查询表达式与普通join子句看上去与之类似,不过却具有完全不同的结果。分组连接
结果中的每个元素由左边序列(使用它的原始范围变量)的某个元素和右边序列的所有匹配元素
的序列组成。分组连接用一个新的范围变量表示,变量由join子句中into后面的标识符指定。
  显示bug和每个bug所需的通知,但将通知按"每个缺陷"进行分解。
  每个条目的Subscriptions属性是一个内嵌序列,该序列包含了匹配该数据项缺陷的所有订阅。
内连接和分组连接之间的一个重要差异(即分组连接和普通分组之间的差异)是,对于分组连接
来说,在左边序列和结果序列之间是一对一的对应关系,即使左边序列中的某些元素在右边序列
中没有任何匹配的元素,也无所谓。这是非常重要的,有时会用于模拟SQL的左外连接。在左边
元素不匹配任何右边元素的时候,嵌入序列就是空的。与内连接一样,分组连接要对右边序列

进行缓冲,而对左边序列进行流处理。


  输出
MP3 files crash system
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Text is too big
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Sky is wrong shade of blue
  talk-bugs@skeetysoft.com
Can't play files more than 200 bytes long
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Installation is slow
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
DivX is choppy on Pentium 100
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Client acts as virus
  talk-bugs@skeetysoft.com
Subtitles only work in Welsh
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Voice recognition is confused by background noise
  talk-bugs@skeetysoft.com
User interface should be more caramelly
  talk-bugs@skeetysoft.com
Burning a CD makes the printer catch fire
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Peer to peer pairing passes parameters poorly
  talk-bugs@skeetysoft.com
Delay when sending message
  talk-bugs@skeetysoft.com
Volume control needs to go to 11
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Splash screen fades too quickly
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Text box doesn't keep up with fast typing
  talk-bugs@skeetysoft.com
Password displayed in plain text
  talk-bugs@skeetysoft.com
Play button points the wrong way
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Wizard needed for CD burning
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Subtitles don't display during fast forward
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Memory leak when watching Memento
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Profile screen shows login count of -1
  talk-bugs@skeetysoft.com
Server crashes under heavy load (3 users)
  talk-bugs@skeetysoft.com
Unable to connect to any media server
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
UI turns black and white when playing old films
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Password reset changes passwords for all users
  talk-bugs@skeetysoft.com
Modern music sounds rubbish
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Webcam makes me look bald
  talk-bugs@skeetysoft.com
Sound is distorted when speakers are underwater
  talk-bugs@skeetysoft.com
Japanese characters don't display properly
  talk-bugs@skeetysoft.com
Video takes 100% of CPU
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
DVD Easter eggs unavailable
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Transparency is high for menus to be readable
  talk-bugs@skeetysoft.com
About box is missing version number
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Logs record confidential conversations
  talk-bugs@skeetysoft.com
Profanity filter is too aggressive
  talk-bugs@skeetysoft.com
Full screen mode fails on dual monitors
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Visualization hypnotises pets
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Resizing while typing loses input
  talk-bugs@skeetysoft.com
Network is saturated when playing WAV file
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com
Media library tells user to keep the noise down
  media-bugs@skeetysoft.com
  theboss@skeetysoft.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值