SQLite和List Generic C#

时间:2012-10-22 15:03:25

标签: c# sqlite

我正在尝试使用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&lt;&gt;功能,但我不能让“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();

 }

希望这能解释这个问题。

由于

1 个答案:

答案 0 :(得分:4)

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);