vinay
vinay

Reputation: 1316

Xamarin ios : Assign a Sqlite Database query results to List

I have a problem with assigning a database query results to List .How can i achieve this?

using (var conn= new SQLite.SQLiteConnection(_pathToDatabase)){
    var query = conn.Table<Person>();
    foreach (var names in query)
        Console.WriteLine ("FirstName: " + names.FirstName);
}

Here is my class Data.cs

using System;

namespace DB
{
    public class Data
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Data ()
        {
        }
    }
}

Need to assign the results using the above class.Please help me.

Upvotes: 1

Views: 3896

Answers (1)

Jason
Jason

Reputation: 89082

List<Data> results = new List<Data>();

using (var conn= new SQLite.SQLiteConnection(_pathToDatabase)){
    var query = conn.Table<Person>();
    foreach (var names in query) {
      var item = new Data();
      item.FirstName = names.FirstName;
      item.LastName = names.LastName;
      item.ID = name.ID;

      results.Add(item);
    }
}

Alternately, if your Person table has the same structure as your Data class, you could get rid of Data and do this

List<Person> results = conn.Table<Person>().ToList<Person>();

Upvotes: 3

Related Questions