Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left Join Linq to Entity's Vb.net

I can't figure out that linq to entity query syntax. My problem is that if the value of the Calls table is null then noting comes up, I want to make something like a left join to get 'all' rows from the Calls table.

I tried to group it but I can't figure out the correct way to write it.

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}

How can I fix that?

like image 990
Ezi Avatar asked Apr 11 '11 22:04

Ezi


1 Answers

Similar question: Linq to Sql: Multiple left outer joins

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

LINQ Examples from: http://msdn.microsoft.com/en-us/vbasic/bb737909

Left Outer Join A so-called outer join can be expressed with a group join. A left outer joinis like a cross join, except that all the left hand side elements get included at least once, even if they don't match any right hand side elements. Note how Vegetables shows up in the output even though it has no matching products.

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
like image 141
Jeremy Avatar answered Oct 12 '22 22:10

Jeremy