2

我正在尝试使用 SQLite 为 Windows 8 转换一些旧代码。下面是上一个会话的有效代码。

using (SqliteConnection conn = new SqliteConnection("Version=3,uri=file://flashcards0904.db"))
        {
            conn.Open();
            using (SqliteCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT * FROM Decks where DeckGroup='" + Global.currentDeckGroup.ToString() + Global.currentDeck.ToString() + "'" + "order by random()";

                List<string> myCollection = new List<string>();

                using (SqliteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        myCollection.Add(reader.GetString(0) + "~" + reader.GetString(1) + "~" + reader.GetString(2));
                        Global.words = myCollection.ToArray();
                    }
                }
                conn.Close();
                Community.CsharpSqlite.FileStream.HandleTracker.Clear();
            }
        }

现在,我无法访问 SqliteDataReader 并且我正在尝试使用 List<> 函数,但我无法让“Where”子句起作用。任何帮助,这是我当前的代码:

var root = Windows.Storage.ApplicationData.Current.LocalFolderPath;
var dbPath= Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolderPath,"flashcards0904.db");

using( var db= new SQLite.SQLiteConnection(dbPath))
{
// Here is location for the missing Where Clause"

    var list= db.Table<Decks>.Where??????? .ToList();

 }

希望这能解释问题。

谢谢

4

1 回答 1

3
var list= db.Table<Decks>.Where(n=>n.DeckGroup == (Global.currentDeckGroup.ToString() + Global.currentDeck.ToString())).ToList()

我鼓励你看看:http ://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

那里有大量示例可以帮助您掌握 LINQ to SQL。

Paolo Moretti 注意到您order by random()在之前的实现中使用过。如果您想在 LINQ 中执行此操作,您可以执行以下操作,如下所示:Linq Orderby random ThreadSafe for use in ASP.NET

Random random = new Random();
int seed = random.Next();
var list= db.Table<Decks>.Where(n=>n.DeckGroup == (Global.currentDeckGroup.ToString() + Global.currentDeck.ToString())).OrderBy(s => (~(s.Shuffle & seed)) & (s.Shuffle | seed)).ToList(); // ^ seed);
于 2012-10-22T15:05:00.390 回答