locked
How to find if all items in a list containing the selected color RRS feed

  • Question

  • Hi
    I'm using the code below to check what color the user has selected. This is only working when my item got one color. Now I would like to change my code so each item can have one or more colors (seperated by semicolon). I guess I have to use string.IndexOf but I don't know how to use it in the .Where scentence below to return all the items that got the selected color.

    Thanks, Sigurd F

    var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
    
    sampleDataGroups = sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color == KeywordColorList.SelectedValue.ToString()).ToList();

    Thursday, April 30, 2015 6:44 AM

Answers

  • If the Color property of the SampleDataGroup class contains a string with different colours separated by a semicolon you could use the following code to determine whether the KeywordColorList.SelectedValue property is amont these colours:

    var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
    
    sampleDataGroups = sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Split(';').Contains(KeywordColorList.SelectedValue.ToString())).ToList();
    

     

    Compilable sample code:

                var sampleDataGroups = new List<SampleDataGroup> 
                {
                    new SampleDataGroup { Color = "blue"},
                    new SampleDataGroup { Color = "blue;red;green"},
                    new SampleDataGroup { Color = "red;green"},
                };
    
                string selectedValue = "blue";
    
                sampleDataGroups = sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Split(';').Contains(selectedValue)).ToList();
    

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Sigurd F Thursday, April 30, 2015 12:26 PM
    Thursday, April 30, 2015 12:19 PM

All replies

  • If the Color property of the SampleDataGroup class contains a string with different colours separated by a semicolon you could use the following code to determine whether the KeywordColorList.SelectedValue property is amont these colours:

    var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
    
    sampleDataGroups = sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Split(';').Contains(KeywordColorList.SelectedValue.ToString())).ToList();
    

     

    Compilable sample code:

                var sampleDataGroups = new List<SampleDataGroup> 
                {
                    new SampleDataGroup { Color = "blue"},
                    new SampleDataGroup { Color = "blue;red;green"},
                    new SampleDataGroup { Color = "red;green"},
                };
    
                string selectedValue = "blue";
    
                sampleDataGroups = sampleDataGroups.Where<SampleDataGroup>(Item => Item.Color.Split(';').Contains(selectedValue)).ToList();
    

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Sigurd F Thursday, April 30, 2015 12:26 PM
    Thursday, April 30, 2015 12:19 PM
  • Thanks a lot :-)

    Regards, Sigurd F

    Thursday, April 30, 2015 12:27 PM