Home > Software engineering >  Filter on subcategories from csv file ---Xamarin forms
Filter on subcategories from csv file ---Xamarin forms

Time:12-17

I want to apply a filter on sub-categories to list the items that match the sub-category. For example, If I pick the "Diamond Men" subcategory, it will show the list of all items with "Diamond Men" as the subcategory. I have tried using Linq but it didn't work. I'm adding the code here with an image of CSV data, Please let me know If you need the whole code I will share the GitHub link. Thank you in advance.

code

Xaml

<Picker x:Name="picker4"
        Title="Select a SubCategory"
        TitleColor="#259" SelectedIndexChanged="picker4_SelectedIndexChanged">
                    <Picker.ItemsSource>
                        <x:Array Type="{x:Type x:String}">

                            <x:String>Men Wedding Band</x:String>
                            <x:String>White Gold Men</x:String>
                            <x:String>Black Diamond Men</x:String>
                         
                        </x:Array>
                    </Picker.ItemsSource>
                </Picker>

            </StackLayout>

            <ListView x:Name="listview2" HasUnevenRows="True"  IsVisible="false" ItemSelected="listview2_ItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="18">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="auto"/>
                                </Grid.ColumnDefinitions>
                                <Label Text="{Binding prod_img}" TextColor="#289" Grid.RowSpan="2" Grid.Column="0" Grid.Row="0" HeightRequest="80"
                       WidthRequest="80" />
                                <Label Text="{Binding prod_name}" TextColor="Black" FontSize="Medium" FontAttributes="Bold"  Grid.Column="1"/>
                                <Label Text="{Binding prod_price}" TextColor="Black" Grid.Row="1"
                       Grid.Column="1" LineBreakMode="TailTruncation"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Xaml.cs

 private Task<List<csv>> filelist()
        {
            var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
            Stream stream = assembly.GetManifestResourceStream("Todolist.WP.csv");
            List<csv> lstSample = new List<csv>();
            using (var reader = new System.IO.StreamReader(stream))
            {
                
                if (reader != null)
                {

                    using (var csvReader = new CsvReader(reader, CultureInfo.CurrentCulture))
                    {
                        while (csvReader.Read())
                        {
                            lstSample.Add(new csv
                            {
                                //prod_id = csvReader.GetField<string>(0),
                                prod_name = csvReader.GetField<string>(2),
                                prod_img = csvReader.GetField<string>(18),
                                prod_price = csvReader.GetField<int>(13),
                                prod_desc = csvReader.GetField<string>(3),
                                prod_m_w = csvReader.GetField<string>(6),
                                Prod_cate= csvReader.GetField<string>(5)
                            });
                            //var todoItem = (task)BindingContext; 
                        }  
                    }
                }
            }
            return Task.FromResult(lstSample);
        }
        
 private void picker4_SelectedIndexChanged(object sender, EventArgs e)
        {
            var jo = filelist().Result;
           


            if(picker3.SelectedIndex == 0)
            {
              //code for filtering here
            }
              

        }

CSV data

CodePudding user response:

Your CSV data contains multiple values in the subcat column, so an exact string comparison will not work. Try this

var result = jo.Where(x => x.Prod_cate.Contains(picker3.SelectedItem)).ToList();
  • Related