【发布时间】:2014-11-07 09:58:39
【问题描述】:
使用任何 NuGet 包:SQLite.Net-PCL - Win32 平台、SQLite.Net-PCL - XamarinIOS 平台或 SQLite.Net-PCL XamarinAndroid 平台我在选择时遇到问题。特别是当我在 LINQ 或原始 SQL 中从数据库中进行选择时,我会返回似乎是包含默认值的对象。
这是一个演示我的问题的示例控制台应用程序:
using System;
using System.Linq;
using SQLite.Net;
using SQLite.Net.Platform.Win32;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Open a connection to the database
using (var database = new SQLiteConnection(new SQLitePlatformWin32(), "db.db"))
{
// Create a simple table
database.CreateTable<Entity>();
// Add a simple record to it each time we start the application
database.Insert(new Entity { Data = Guid.NewGuid().ToString(), BoolData = true, IntData = 5 });
Console.WriteLine("---------> Inserted item:");
// Display all our records
foreach (var e in database.Table<Entity>())
{
Console.WriteLine(e);
}
Console.WriteLine(Environment.NewLine);
Console.WriteLine("---------> Linq select Ids:");
// For every record we'll select the Id field - this is not working
foreach (var e in database.Table<Entity>().Select(e => e.Id))
{
Console.WriteLine(e);
}
Console.WriteLine(Environment.NewLine);
Console.WriteLine("---------> Id by scalar query:");
// Let's try going after a value explicitly - this is fine
var r1 = database.ExecuteScalar<int>("SELECT Id FROM Entity WHERE Id == 1");
Console.WriteLine(r1);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("---------> Ids by query:");
// So lets try going after our Id field from a query - this still dosen't work
foreach (var e in database.Query<int>("SELECT Id FROM Entity"))
{
Console.WriteLine(e);
}
Console.WriteLine(Environment.NewLine);
Console.WriteLine("---------> Linq select Ids after force to memory:");
// Now lets try forcing a where to execute before performing the select - this works but it's bad
foreach (var e in database.Table<Entity>().Where(e => e.IntData == 5).ToList().Select(e => e.Id))
{
Console.WriteLine(e);
}
Console.ReadKey();
}
}
}
}
Entity 只是一个简单的 POD:
using SQLite.Net.Attributes;
namespace ConsoleApplication1
{
public class Entity
{
public Entity() { }
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Data { get; set; }
public int IntData { get; set; }
public bool BoolData { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Data: {1}, IntData: {2}, BoolData: {3}", Id, Data, IntData, BoolData);
}
}
}
因此,我能够使其工作的唯一方法是首先将 where 强制放入内存中......在我们可能拥有包含我们不感兴趣的 BLOB 等数据的情况下,这很糟糕。
这种行为是故意的还是我错过了什么?
【问题讨论】:
标签: c# sqlite xamarin cross-platform portable-class-library