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) { }
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没
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 里应该 怎么写 才能 得到 左连接后的值呢
@梁娜: new { OwnerName = person, Pet = pet });保存连接后的到新的自对象名。然后FOREACH里面可以获得OwnerName.属性名 Pet.属性名
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 };
看看,我不说话