从csv文件筛选子类别---Xamarin表单

hgqdbh6s  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(327)

我想在子类别上应用一个过滤器,以列出与子类别匹配的项目。例如,如果我选择"钻石男人"子类别,它将显示所有项目的列表与"钻石男人"作为子类别。我已经尝试使用Linq,但它不工作。我在这里添加的代码与CSV数据的图像,请让我知道,如果你需要整个代码,我会分享GitHub链接。谢谢你提前。

    • 代码**
    • 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>
    • 检验报告**
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

9wbgstp7

9wbgstp71#

您的CSV数据在subcat列中包含多个值,因此无法进行精确的字符串比较。

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

相关问题