Answered by:
How to filter a List<>

Question
-
Hi!
How can I filter a List<> by one or two keywords? I would like to filter out all flowers that is yellow. I have tried the code below, but with no success.
this.DefaultViewModel["Groups"] = sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Substring(0) == "yellow").ToString();
More info about my code on this page:
Wednesday, April 22, 2015 8:32 PM
Answers
-
HI Sigurd
You shouldn't call "ToString()" at the end. Instead call "ToList()"
sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Substring(0) == "yellow").ToList();
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-courses:
XAML Layout in Depth
Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com- Marked as answer by Sigurd F Wednesday, April 22, 2015 9:22 PM
Wednesday, April 22, 2015 8:48 PM -
You can just add a and-condition with &&, like for example:
sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Substring(0) == "yellow" && Item.Location.Substring(0) == "redmond").ToList();
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-courses:
XAML Layout in Depth
Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com- Marked as answer by Sigurd F Thursday, April 23, 2015 8:30 PM
Thursday, April 23, 2015 6:17 PM
All replies
-
HI Sigurd
You shouldn't call "ToString()" at the end. Instead call "ToList()"
sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Substring(0) == "yellow").ToList();
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-courses:
XAML Layout in Depth
Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com- Marked as answer by Sigurd F Wednesday, April 22, 2015 9:22 PM
Wednesday, April 22, 2015 8:48 PM -
Yes, of cource. Thanks. I should have spotted that one :-)
Regards, Sigurd F
Wednesday, April 22, 2015 9:22 PM -
One more question: how do I write the code for filtering for more than one thing? I would like to filter on both color and location.
Thanks, Sigurd F
Wednesday, April 22, 2015 9:59 PM -
You can just add a and-condition with &&, like for example:
sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Substring(0) == "yellow" && Item.Location.Substring(0) == "redmond").ToList();
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-courses:
XAML Layout in Depth
Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com- Marked as answer by Sigurd F Thursday, April 23, 2015 8:30 PM
Thursday, April 23, 2015 6:17 PM