UWP SQLite 查询结果 class 包含另一个 class

UWP SQLite query result class contains another class

我知道我们可以进行多表查询,如下所示:

using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), App.DB_PATH))
{
    var result = db.Query<PersonWithAddress>(
        @"SELECT Person.Id, Person.Name, Person.Surname, 
            Address.Street, Address.City, Address.Country
        FROM Person INNER JOIN Address ON Person.AddressId = Address.Id;");
}

private class PersonWithAddress
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

*参考自

但是我需要结果如何 class PersonWithAddress 应该是:

private class PersonWithAddress
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public Address mAddress { get; set}
}

private class Address {
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

我正在寻找 DataReader 的方法,但 UWP 上的 SQLite 似乎不支持它。

这里有一个Portable Class Library for SQLite提供类似于DataReader的方法。

要使用这个库,我们可以从 NuGet 安装它,然后像下面这样使用它:

List<PersonWithAddress> PersonWithAddressList = new List<PersonWithAddress>();

using (var connection = new SQLitePCL.SQLiteConnection(DB_PATH))
{
    using (var statement = connection.Prepare(@"SELECT Person.Id, Person.Name, Person.Surname, Address.Street, Address.City, Address.Country FROM Person INNER JOIN Address ON Person.AddressId = Address.Id;"))
    {
        while (statement.Step() == SQLitePCL.SQLiteResult.ROW)
        {
            var personWithAddress = new PersonWithAddress();
            personWithAddress.Id = Convert.ToInt32(statement[0]);
            personWithAddress.Name = (string)statement[1];
            personWithAddress.Surname = (string)statement[2];
            personWithAddress.mAddress.Street = (string)statement[3];
            personWithAddress.mAddress.City = (string)statement[4];
            personWithAddress.mAddress.Country = (string)statement[5];

            PersonWithAddressList.Add(personWithAddress);
        }
    }
}

更多信息可以参考这篇博客:The new Portable Class Library for SQLite.

多表查询的另一种方式是使用SQLite-Net Extensions, you can specify the relationships in the entities. For example, in your project, add a reference to SQLiteNetExtensions NuGet package. Also available with Async support in SQLiteNetExtensions.Async NuGet package。然后像下面这样改变你的class:

public class Person
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Address))]
    public int AddressId { get; set; }

    public string Name { get; set; }
    public string Surname { get; set; }

    [ManyToOne]
    public Address mAddress { get; set; }
}

public class Address
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Street { get; set; }

    public string City { get; set; }
    public string Country { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<Person> Persons { get; set; }
}

在此之后,我们可以使用以下代码获取所有地址为:

的人
using (var connection = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DB_PATH))
{
    List<Person> PersonsWithAddress = connection.GetAllWithChildren<Person>();
}