左 加入 Linq 到 实体的 Vb.net



我不明白linq到实体的查询语法。我的问题是,如果调用表的值为空,然后注意出现,我想做一个类似于左连接的东西,从调用表中获取"所有"行。

我试着把它归类,但我想不出正确的写法。

Dim TicketQuery As ObjectQuery = From c In EnData.Customer _
                                         Join t In EnData.Calls On t.CustomerID Equals c.CustomerID _
                                         Join Status In EnData.Lists On t.Status Equals Status.ListValue _
                                         Join Project In EnData.Lists On t.Project Equals Project.ListValue _
                                         Join Priorty In EnData.Lists On t.Priority Equals Priorty.ListValue _
                                         Where c.Status > -1 And t.Status > -1 And Status.ListType = 1 And Project.ListType = 3 And Priorty.ListType = 2 _
         Select New With {c.CustName, t.CallID, t.CallDate, t.CallTime, t.Description, Key .Status = Status.ListText, Key .Project = Project.ListText, t.DateModified, Key .Priority = Priorty.ListText}

我怎样才能解决这个问题?

类似的问题:Linq to Sql:多个左外连接

Microsoft Documentation: http://msdn.microsoft.com/en-us/library/bb918093.aspx#Y916

LINQ示例来自:http://msdn.microsoft.com/en-us/vbasic/bb737909

左外连接所谓的外连接可以用组连接表示。左外部连接类似于交叉连接,除了所有左侧元素至少包含一次,即使它们与任何右侧元素不匹配。请注意,即使没有匹配的产品,Vegetables也会显示在输出中。

Public Sub Linq105()
    Dim categories() = {"Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood"}
Dim productList = GetProductList()
Dim query = From c In categories _
            Group Join p In productList On c Equals p.Category Into Group _
            From p In Group.DefaultIfEmpty() _
            Select Category = c, ProductName = If(p Is Nothing, "(No products)", p.ProductName)
For Each v In query
    Console.WriteLine(v.ProductName + ": " + v.Category)
Next
End Sub

对于VB.net中的左连接,我们可以使用Let

Dim q = 
    (From item In _userProfileRepository.Table
     Let Country = (From p In _countryRepository.Table Where p.CountryId = item.CurrentLocationCountry Select p.Name).FirstOrDefault
     Let State = (From p In _stateRepository.Table Where p.CountryId = item.CurrentLocationCountry Select p.Name).FirstOrDefault
     Let City = (From p In _stateRepository.Table Where p.CountryId = item.CurrentLocationCountry Select p.Name).FirstOrDefault
    Where item.UserId = item.ProfileId.ToString)

左连接后可以使用group by

Dim q2 = 
    (From p In q Group p By p.Country, p.State, p.City Into Group 
     Select New With 
         {
             .Country = Country, 
             .State = State, 
             .City = City, 
             .Count = Group.Count
         }).ToList()

相关内容

  • 没有找到相关文章

最新更新