Resolved Lambda Expression to test two values

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hi guys, i'm looking to improve some code and get a little bit more specific with it.
search.png
The CheckedListBox holds a list of cars and the DataGridView shows the cars Car.Orders

Here's the code i'm using, this code doesn't do everything i need it to, but it worked to allow me to continue creating and finish the form.

Should i store the Return results into a variable then bind it or is it just the same making the data source the direct results?

EDIT : Sorry i really should have mentioned im Using Entity Framework.

I also want to test each car for 3 values:
1: Is the car fully paid(if it has no outstanding work to be invoiced then no point testing it for anything else) Car.FullyPaid = False

2: Does it Have Any orders for this customer,(again if no orders exist for the customer, then no point in searching further) Car.Orders.Department_ID = Invoice.Department_ID, im pretty sure i have to use lambda if not please advise

3: If the order belongs to the selected customer, has it been Order.Invoiced,(im not worried if the order has been paid, if it has been invoiced then it needs chased up, not put onto a new invoice)

i tried searching through the orders and selecting the Order.Car but it would add the car each time their was an order that passed, ending up in the same car being added multiple times
'Me.clbCars.DataSource = From Order As Order In db.Orders Where Order.Department_ID = Invoice.Department_ID Select Order.Car

VB.NET:
        'For the moment, selects the cars that have ordernumbers for the selected customer(department)
        Me.clbCars.DataSource = From Car In db.Cars Where Car.FullyPaid = False AndAlso Car.Orders.Any(Function(ord As Order) ord.Department_ID = Invoice.Department_ID)

and Finally, the search results then adds the cars to the CheckedListBox and the DataGridView shows the cars orders, but it is show all the orders not just the orders that passed the search, i understand why this is happening, but any idea how i can refine the search to do these things.

sorry for big post, but i always try to make it very clear what i'm trying to do and what is happening

Thanks in advance for any reply's and help
 
Last edited:
UPDATE
Hi guys, tried this but i got an error message. Collection was modified; enumeration operation may not execute.
VB.NET:
Dim listofcars As List(Of Car) = (From Car As Car In db.Cars Where Car.FullyPaid = False AndAlso Car.Orders.Any(Function(ord As Order) ord.Department_ID = invoice.Department_ID) 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
 

Latest posts

Back
Top