Lambda expression question
Hi!
I have a List<SearchCriteria> variable which basically store a list of searchitems. Each item has a Value property which is a string.
Now when I want to use this list to search, I need to set certain values, for example I want to show only used cars, I have a value for this. I tried to write this lambda expression to do this but I got error "cannot implicitly convert type string to bool
Code:
this.CarsSearchCriteria.Where(n => n.CriteriaId == SearchCriterias.IsUsed).First(m => m.Value = m_ShowUsedCars.ToString());
To me, this lambda is pretty straight forward. I look up the ceiteria item called IsUsed, and set the value to a local member on my form. But some how it doesnt work. What am I doing wrong? Perhaps there is a better way to do this?
Re: Lambda expression question
what is the type of CriteriaId ?
.IsUsed property suggests to me it is a bool value, but creteriaID is of a different type... maybe string?
Re: Lambda expression question
Hi!
SearchCriterias.IsUsed is an enum
and CriteriaID is of the same enum type.
Basically a SearchCriteriaItem is a generic object that can store any kind of criteria. The criteria are differentied by their CriteriaID which is the name of the criteria. In this case the criteria I want to alter is the one that allow me to search for used cars, hence the "IsUsed" name.
The compiler complains not at that but here, where I set the criteria value:
m.Value = m_ShowCleared
I assume == is compare and = is assign even in lambdas?
kind regards
Henrik
Re: Lambda expression question
Perhaps it is helpful if I describe generally what I try to do
Imagine you have a List<Car>
the class car is described like this
class car
{
String brand;
int year;
double price;
}
Now Imagine that I have 20 cars in my list. And now I want to create a lambda expression that set the price of all cars with brand=="Volvo" to 4000.
This is the same thing as I try to do above.
I tried btw to replace the m_ShowCleared with a string like "Foo" but I get the same error. I thhink the expression is written with some other error.
Re: Lambda expression question
I dont think you can set a different value on variables in a lambda/query expression as you are doing now, what you are doing is simply trying to select results of a criteria. this is what the .Where extension method is used for....
I am not entirely sure how to update the property with a new value in LINQ to Objects/vica versa but I do know it is possible to do.
to do this, you maybe able to do something like the following:
var updatedItems = myList.Select(x => { myProperty.Value = newValue; return x; } );
this is using an anonymous method within the lambda expression, stating to select the items, and update the current item with a newvalue and returning it back to the caller.
so once you have filtered your items, simply update those items with the new value as shown above.
again, not a 100% correct but im sure the above will give you hints somewhere
Re: Lambda expression question
I think you're unclear what the First method does. It returns the first item from a sequence that optionally matches a specific condition. The lambda you pass to First MUST evaluate to a bool so that the method can return the first item for which that bool is true. If you want to edit the first item in that sequence then you have to get the object that is returned by the First method and then edit it.
Also, there's no point calling Where and First. This:
Code:
something.Where(someLambda).First()
is the same as this:
Code:
something.First(someLambda)
What you want is this:
Code:
this.CarsSearchCriteria.First(n => n.CriteriaId == SearchCriterias.IsUsed).Value = m_ShowUsedCars.ToString();
That gets the first item in the sequence for which the CriteriaId property compares truly to the IsUsed value and then sets the Value property of that item to the string representation of m_ShowUsedCars.