首页 > 解决方案 > 使用 SearchBar 过滤 SQLite 数据库

问题描述

我已经能够生成并显示一个 SQList 数据库,现在我想用搜索栏过滤它,我已经阅读了很多教程,但我无法让它工作......

我有一个带有以下代码的 SQLite 模型:

using SQLite;
namespace AppListo
{
public class Employee
  {
    [PrimaryKey, AutoIncrement]
    public long EmpId
    { get; set; }
    [NotNull]
    public string EmpName
    { get; set; }

  }
}

...我从数据库中检索数据列表:

public List<Employee> GetAllEmployees()
    {
        return dbConn.Query<Employee>("Select * From [Employee]");
    }

...我在 ContentPage 中使用此 xaml 代码显示列表:

<ListView x:Name="lstData" HasUnevenRows="false" Header="Header Value" Footer="Footer" ItemSelected="OnSelection" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="5,5,5,5">
                            <Label Text="{Binding EmpName}" FontSize="Medium" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

...以及后面的代码:

var vList = App.DAUtil.GetAllEmployees();
lstData.ItemsSource = vList;

...但我不知道如何使用搜索栏过滤此列表。你能帮我实现它吗?

提前致谢

标签: c#xamlxamarin.forms

解决方案


使用 LINQ

var vList = App.DAUtil.GetAllEmployees();
lstData.ItemsSource = vList.Where(e => e.Name.Contains(mySearchString));

推荐阅读