Search code examples
c#maui

NET Maui - How do I run a SQLight query with parms and populate a List of objects?


I am using SQLight from these packages. SQLite-net-pcl - 1.9.172 SQLitePCLRaw.bundle_green - 2.1.8

I have the following working:

SQLiteConnection Database = new SQLiteConnection(Constants.DatabasePath, Constants.Flags);

public class Location
{
    public string LocationID { get; set; }
    public string LocationName { get; set; }
}
List<Location> Locations    = new List<Location>();

String loc = "1290";
Locations = Database.Query<Location>("Select * from Location where LocationID = "+loc).ToList();

// Event is a Picker
Event.ItemsSource = Locations;
Event.ItemDisplayBinding = new Binding("LocationName");

How do I handle SQL parms in Maui instead of "Select * from Location where LocationID = "+loc


Solution

  • Use LINQ

    Locations = Database.Table<Location>().Where(l => l.LocationID == loc).ToList();