左连接Linq到实体';s Vb.net
我无法理解linq到实体查询的语法。我的问题是,如果Calls表的值为null,那么就不会出现任何问题,我希望进行类似于左连接的操作,以从Calls表中获取“all”行 我试着把它分组,但我想不出正确的写作方法左连接Linq到实体';s Vb.net,vb.net,linq,entity-framework,Vb.net,Linq,Entity Framework,我无法理解linq到实体查询的语法。我的问题是,如果Calls表的值为null,那么就不会出现任何问题,我希望进行类似于左连接的操作,以从Calls表中获取“all”行 我试着把它分组,但我想不出正确的写作方法 Dim TicketQuery As ObjectQuery = From c In EnData.Customer _ Join t In EnData.Calls On t.CustomerID Equ
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}
我如何解决这个问题?类似的问题:
Microsoft文档:
LINQ示例来自:
左外连接
所谓的外部联接可以用组联接来表示。左侧外部连接类似于交叉连接,只是所有左侧元素至少包含一次,即使它们与任何右侧元素都不匹配。注意蔬菜是如何显示在输出中的,即使它没有匹配的产品
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)
在左连接之后,我们可以使用GROUPBY
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()
你能解释一下你想看到的结果吗?如果你创建了虚拟类,你可以在LINQPad中测试你的LINQ