首页 新闻 搜索 专区 学院

c# System.Linq 下的join 左连接 怎么写

0
悬赏园豆:10 [已解决问题] 解决于 2013-11-28 13:17

c# System.Linq 下的join 左连接 怎么写

 

我会写 内链接

 var a = AttendanceAndItemListModel.Instance.AttendanceAndItemList.AttendanceItemList.Join(
                     AttendanceFormulaListModel.Instance.AttendanceFormulaList,
                     (i) => { return i.AttendanceCode; },
                     (f) => { return f.ItemCode; },
                     (i, f) => new { i, f });

                foreach (var item in a)
                {
                    
                }
c#
梁娜的主页 梁娜 | 初学一级 | 园豆:39
提问于:2013-11-19 14:49
< >
分享
最佳答案
0
var query = db.PictureAblums.GroupJoin(db.AblumComments,
pa=> pa.K,
ac=> ac.Ablumk,
(pa, ac) =>
new { OwnerName = person, Pet = pet });

 

对应生成的SQL语句为

SELECT 
[Project1].[K] AS [K], 
[Project1].[Id] AS [Id], 
[Project1].[UserId] AS [UserId], 
[Project1].[Name] AS [Name], 
[Project1].[AblumPath] AS [AblumPath], 
[Project1].[Content] AS [Content], 
[Project1].[AblumType] AS [AblumType], 
[Project1].[CreateTime] AS [CreateTime], 
[Project1].[UserName] AS [UserName], 
[Project1].[C1] AS [C1], 
[Project1].[K1] AS [K1], 
[Project1].[Ablumk] AS [Ablumk], 
[Project1].[Comment] AS [Comment], 
[Project1].[UserId1] AS [UserId1], 
[Project1].[SupportNumber] AS [SupportNumber], 
[Project1].[UserName1] AS [UserName1], 
[Project1].[CommentTime] AS [CommentTime]
FROM ( SELECT 
    [Extent1].[K] AS [K], 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[AblumPath] AS [AblumPath], 
    [Extent1].[Content] AS [Content], 
    [Extent1].[AblumType] AS [AblumType], 
    [Extent1].[CreateTime] AS [CreateTime], 
    [Extent1].[UserName] AS [UserName], 
    [Extent2].[K] AS [K1], 
    [Extent2].[Ablumk] AS [Ablumk], 
    [Extent2].[Comment] AS [Comment], 
    [Extent2].[UserId] AS [UserId1], 
    [Extent2].[SupportNumber] AS [SupportNumber], 
    [Extent2].[UserName] AS [UserName1], 
    [Extent2].[CommentTime] AS [CommentTime], 
    CASE WHEN ([Extent2].[K] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[PictureAblums] AS [Extent1]
    LEFT OUTER JOIN [dbo].[AblumComments] AS [Extent2] ON [Extent1].[K] = [Extent2].[Ablumk]
)  AS [Project1]
ORDER BY [Project1].[K] ASC, [Project1].[C1] ASC

看到那个LEFT OUT JOIN没

收获园豆:7
大芝麻 | 初学一级 |园豆:4 | 2013-11-19 17:47
     var a = AttendanceItemList.GroupJoin(
                     AttendanceFormulaList,
                     (i) => i.AttendanceCode,
                     (f) => f.ItemCode,
                     (i, f) => new { i = i, f = f });

                foreach (var attenitem in a)
                {}

我的AttendanceItemList、AttendanceFormulaList 这两个集合是 通过i.AttendanceCode=f.ItemCode left join 的 即 AttendanceItemList left join AttendanceFormulaList on i.AttendanceCode=f.ItemCode

那么 我foreach 里应该 怎么写 才能 得到 左连接后的值呢

梁娜 | 园豆:39 (初学一级) | 2013-11-25 14:26

@梁娜: new { OwnerName = person, Pet = pet });保存连接后的到新的自对象名。然后FOREACH里面可以获得OwnerName.属性名   Pet.属性名

大芝麻 | 园豆:4 (初学一级) | 2013-11-26 21:15
其他回答(2)
0
var sqlresult = from b in book  joins in students on b.borroeStudentNoequalss.No intobs  from bb in bs.DefaultIfEmpty()  select new  {  b.bookName,  s.No,  s.Name  }; 
收获园豆:3
happydaily | 园豆:291 (菜鸟二级) | 2013-11-19 17:33
0

看看,我不说话

男人要爽 | 园豆:15 (初学一级) | 2013-11-19 22:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册