So I am abit stuck on a linq statement below,
public void ReturnHire(string carregistration, string hirefromdate, string hiretodate)
{
var result = customermembers.Where(c => c.CustomerCarType.Where(n => String.Equals(n.CarRegistration, carregistration)).FirstOrDefault(); // this line
if (result != null)
{
customermembers.ForEach(c => c.CustomerHireDate.RemoveAll(cs => cs.HireFromDate == hirefromdate && cs.HireToDate == hiretodate));
customermembers.ForEach(c => c.CustomerCarType.RemoveAll(cs => cs.CarRegistration == carregistration));
}
}
My xml looks like this:
<ArrayOfCustomer>
<Customer>
<CustomerID>1</CustomerID>
<FirstName>G</FirstName>
<LastName>Graam</LastName>
<Age>27</Age>
<CustomerHireDate>
<HireDate>
<HireFromDate>15.07.2012</HireFromDate>
<HireToDate>29.07.2012</HireToDate>
</HireDate>
</CustomerHireDate>
<CustomerCarType>
<CarType>
<CarRegistration>IAWB-OOTO</CarRegistration>
</CarType>
</CustomerCarType>
</Customer>
</ArrayOfCustomer>
On my client side the customer is to “return” the vehicle, for me this is basically just removing the hire to and from dates and removing the registration number from within the customer. But here is the catch I have to do this deletion with just the registration number so I was trying to do a where within a where claus but it says I cant implcitly convert CarType to bool.
My web service has these lists:
List<Customer> customermembers = new List<Customer>();
List<HireDate> hiredates = new List<HireDate>();
List<CarType> cartypes = new List<CarType>();
I just thought customermembers where customer car type where rehistration number equals mystring would have been fine?
Since you don’t actually use
resultin your if block, you could do this:Anyreturns true if there is at least one element that satisfies the predicate.This is not terribly efficient, however, since you’re iterating the list three times; you should iterate it just once. I would post an example, but I think there’s a bug in your code and I don’t want to repeat it. Consider separate customers with separate registrations with the same hirefromdate and hiretodate. Your code would remove the CustomerHireDate from the customer who did not yet return the car.
However, you may well have intended to use
resultin the if block, which should fix the bug:Remember that
Wheredoes not change the original collection, so you need to iterate overresultin order to get the filtered set of objects.