Question Filtered Select statement on Entity Framework

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
Hi guys, got my lambda expression fixed as in i didn't need them, never realized i could use two arguments for comparison.

I have a CheckedListBox, that holds a Collection of Cars, And a DataGridView That Displays The Cars.Orders


The Problem i am getting, is the a common problem, that people are complaining that you cant use a Where Clause with an Include Statement(lots of hours googling & trying & failing)

After getting my list of cars, it is displaying ALL orders related to that car.

VB.NET:
Dim listofcars = From car As Car In db.Cars, Ord As Order In car.Orders  Where car.FullyPaid = False AndAlso Ord.Department_ID =  invoice.Department_ID AndAlso Ord.Invoiced = False Select car

Here is code i have been trying and messing with to no avail

VB.NET:
'Tried  to get a new list of cars and loop through to remove orders(didn't work, stopped because collection was edited)
 Dim  listofcars = (From car As Car In db.Cars, Ord As Order In car.Orders  Where car.FullyPaid = False AndAlso Ord.Department_ID =  invoice.Department_ID AndAlso Ord.Invoiced = False Select car).ToList
'For Each Car As Car In listofcars
        '    'Loops through the orders to check if they are suitable for invoicing
        '    For Each Ord As Order In Car.Orders
        '        If Ord.Department_ID <> invoice.Department_ID Then
        '            Car.Orders.Remove(Ord)
        '            Continue For
        '        End If

        '        If Ord.Invoiced = True Then
        '            Car.Orders.Remove(Ord)
        '            Continue For
        '        End If
        '    Next

        '    'Checks for any orders remaining after filtering
        '    If Not Car.Orders.Any Then listofcars.Remove(Car)
        'Next


         'Dim query = db.Orders.Where(Function(ord As Order) ord.Invoiced =  False AndAlso ord.Department_ID = invoice.Department_ID)


        'Dim query = From Car As Car In db.Cars
        '            Where Car.FullyPaid = False _
         '            Select New Car With {.Reg = Car.Reg, .Make = Car.Make,  .Model = Car.Model, .Colour = Car.Colour, .AmountTotal = Car.AmountTotal  _
        '                                , .AmountDue =  Car.AmountDue, .AmountPaid = Car.AmountPaid, .FirstCreatedDate =  Car.FirstCreatedDate, _
        '                                 .LastEditDate = Car.LastEditDate, .FullyPaid = Car.FullyPaid, _
         '                                 .Orders = (From Ord In Car.Orders  Where Ord.Invoiced = False AndAlso Ord.Department_ID =  invoice.Department_ID Select New With{Key )}

Didn't Work, Cant Compair Boolean? with Boolean(dont know why
        Dim query = db.Cars.Where(Function(c) c.FullyPaid = False).Select(Function(c) New With {Key .car = c, Key .Orders = _
  c.Orders.Where(Function(ord) ord.Department_ID = invoice.Department_ID)})
        Dim results = query.ToList()
        Dim products = results.Select(Function(x) x.car)
 
Last edited:
It would help if you would detail exactly what you want and what you are working with. A definition of the relationships of your datasource for the concerned tables would also help. Linq is just a way to represent an expression, what is the expression here?
 
Hi guys, just to make it noted for anyone else that has this problem and comes across the thread, this is what i did..

VB.NET:
 'Gets List Of ALL Cars From Dealer With An OrderNumber From That Department
        Dim listofcars = (From car As Car In db.Cars, Ord As Order In car.Orders Where car.FullyPaid = False AndAlso Ord.Department_ID = invoice.Department_ID AndAlso Ord.Invoiced = False Select car).Distinct

        Me.clbCars.DataSource = listofcars
        Me.clbCars.DisplayMember = "Reg"

    End Sub

    Private Sub clb_Cars_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles clbCars.SelectedIndexChanged
        Dim car As Car = CType(Me.clbCars.SelectedItem, Car)
        OrdersBindingSource.DataSource = car.Orders.Where(Function(ord) ord.Department_ID = invoice.Department_ID AndAlso ord.Invoiced = False)
 
Back
Top