当前位置:  开发笔记 > 编程语言 > 正文

Linq to Sql:多个左外连接

如何解决《LinqtoSql:多个左外连接》经验,为你挑选了4个好方法。



1> Amir..:

这可能更干净(你不需要所有的into陈述):

var query = 
    from order in dc.Orders
    from vendor 
    in dc.Vendors
        .Where(v => v.Id == order.VendorId)
        .DefaultIfEmpty()
    from status 
    in dc.Status
        .Where(s => s.Id == order.StatusId)
        .DefaultIfEmpty()
    select new { Order = order, Vendor = vendor, Status = status } 
    //Vendor and Status properties will be null if the left join is null

这是另一个左连接示例

var results = 
    from expense in expenseDataContext.ExpenseDtos
    where expense.Id == expenseId //some expense id that was passed in
    from category 
    // left join on categories table if exists
    in expenseDataContext.CategoryDtos
                         .Where(c => c.Id == expense.CategoryId)
                         .DefaultIfEmpty() 
    // left join on expense type table if exists
    from expenseType 
    in expenseDataContext.ExpenseTypeDtos
                         .Where(e => e.Id == expense.ExpenseTypeId)
                         .DefaultIfEmpty()
    // left join on currency table if exists
    from currency 
    in expenseDataContext.CurrencyDtos
                         .Where(c => c.CurrencyID == expense.FKCurrencyID)
                         .DefaultIfEmpty() 
    select new 
    { 
        Expense = expense,
        // category will be null if join doesn't exist
        Category = category,
        // expensetype will be null if join doesn't exist
        ExpenseType = expenseType,
        // currency will be null if join doesn't exist
        Currency = currency  
    }


@manitra:不,你实际上得到了LEFT OUTER JOIN语句(没有嵌套选择).很疯狂吧?
这是各种甜蜜的.但是:wtf为什么在linq中没有左连接,如果有连接?什么基于集合的世界只做内部联接?哎呀.
我比使用所有的into语句更喜欢这种方法.感谢您发布此内容!
这只是在我的脸上露出灿烂的笑容.感谢您提供易于理解的示例.
我试过了,这比@tvanfosson的方法慢了一个数量级。我不是直接针对数据库,而是严格按照对象进行操作。我有相当于500000的费用,4000 categoryDtos和4000enseTypeDtos。运行了1分钟。使用tvanfosson的语法,需要6秒钟。

2> tvanfosson..:

无法访问VisualStudio(我在Mac上),但使用http://bhaidar.net/cs/archive/2007/08/01/left-outer-join-in-linq-to中的信息-sql.aspx看起来你可能会做这样的事情:

var query = from o in dc.Orders
            join v in dc.Vendors on o.VendorId equals v.Id into ov
            from x in ov.DefaultIfEmpty()
            join s in dc.Status on o.StatusId equals s.Id into os
            from y in os.DefaultIfEmpty()
            select new { o.OrderNumber, x.VendorName, y.StatusName }



3> Bryan Roth..:

我想出了如何使用LINQ to SQL在VB.NET中使用多个左外连接:

Dim db As New ContractDataContext()

Dim query = From o In db.Orders _
            Group Join v In db.Vendors _
            On v.VendorNumber Equals o.VendorNumber _
            Into ov = Group _
            From x In ov.DefaultIfEmpty() _
            Group Join s In db.Status _
            On s.Id Equals o.StatusId Into os = Group _
            From y In os.DefaultIfEmpty() _
            Where o.OrderNumber >= 100000 And o.OrderNumber <= 200000 _
            Select Vendor_Name = x.Name, _
                   Order_Number = o.OrderNumber, _
                   Status_Name = y.StatusName



4> Mitul..:

在VB.NET中使用Function,

Dim query = From order In dc.Orders
            From vendor In 
            dc.Vendors.Where(Function(v) v.Id = order.VendorId).DefaultIfEmpty()
            From status In 
            dc.Status.Where(Function(s) s.Id = order.StatusId).DefaultIfEmpty()
            Select Order = order, Vendor = vendor, Status = status 

推荐阅读
勤奋的瞌睡猪_715
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有