加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – Linq to Sql:多个左外连接

发布时间:2020-12-17 07:34:20 所属栏目:百科 来源:网络整理
导读:我有一些麻烦,弄清楚如何使用LINQ to SQL使用多个左外连接。我理解如何使用一个左外连接。我使用VB.NET。下面是我的SQL语法。 T-SQL SELECT o.OrderNumber,v.VendorName,s.StatusNameFROM Orders oLEFT OUTER JOIN Vendors v ON v.Id = o.VendorIdLEFT OUTE
我有一些麻烦,弄清楚如何使用LINQ to SQL使用多个左外连接。我理解如何使用一个左外连接。我使用VB.NET。下面是我的SQL语法。

T-SQL

SELECT
    o.OrderNumber,v.VendorName,s.StatusName
FROM
    Orders o
LEFT OUTER JOIN Vendors v ON
    v.Id = o.VendorId
LEFT OUTER JOIN Status s ON
    s.Id = o.StatusId
WHERE
    o.OrderNumber >= 100000 AND
    o.OrderNumber <= 200000
这可能更干净(你不需要所有的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  
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读