situation : i have a event handler of a search textbox in MainPage.xaml.cs
void src_textbox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
listBoxTextItems.ItemsSource = App.ViewModel.Problems.Where(w => w.ProblemName.ToLower().Contains(tb.Text));
}
listBoxTextItems : name of listbox control ,
Problems : name of collection instantiated in MainViewModelClass constructor ,
this code shows only those objects whose ProblemName
property contains the letters typed in textbox .
Problem : what i want is that this code should also show those objects whose ProblemDesc
property contains the letters typed in textbox .
i tried something like this :
listBoxTextItems.ItemsSource = App.ViewModel.Problems.Where((w => w.ProblemName.ToLower().Contains(tb.Text))||(w => w.ProblemDesc.ToLower().Contains(tb.Text)));
but i get an error saying "operator ||
cannot be applied to operands of type lambda expression
and lambda expression
. can anyone suggest me correct code ?
You don't need to specify lambda parameter whenever you need to use it. Just specify it once:
App.ViewModel.Problems.Where(w => w.ProblemName.ToLower().Contains(tb.Text) ||
w.ProblemDesc.ToLower().Contains(tb.Text));
When you specify w
second time, you are creating a new lambda expression therefore you get the error.Simple syntax of lambda expression is:
(input parameters) => expression
In this case you have only one input parameter which is w
, you need to specify it only once so you will be using the same parameter and create only one lambda expression which returns a boolen result.
You can refer to documentation for more detailed explanation about lambda expressions: