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

前端之家收集整理的这篇文章主要介绍了vb.net – Linq to Sql:多个左外连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些麻烦,弄清楚如何使用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  
    }

猜你在找的VB相关文章