0
我試圖循環SQL查詢結果do-while循環,但是存在結果變量索引問題。SQL查詢結果錯誤:查詢運算符'ElementAt'不受支持
的問題是與while (selectQuery.ElementAt(i).Source != user.Item1 && selectQuery.ElementAt(i).UserID != user.Item2 && i<selectQuery.Count());
所以我的問題是該行:我怎能指數的SQL查詢結果的變量?
這裏是我的代碼:
//------------------Database connection initialization--------------------------
string m_dbConnestionString = "Server=aaa; Database=bbb; uid=ccc; pwd=ddd; Trusted_Connection=True;"
DataClasses1DataContext db = new DataClasses1DataContext(m_dbConnectionString);
Table<Central> m_myTable = db.GetTable<Central>()
// -----------------------------------------------------------------------------
int i = -1;
var selectQuery =
from central in m_myTable
select central;
foreach (var row in selectQuery)
{
Console.WriteLine("{0}; {1}",row.DisplayName, row.Email);
}
foreach (var user in allMailusers) // btw, allMailUsers is List<Tuple<string, string, string, string, string>>
{
do
{
i += 1;
} while (selectQuery.ElementAt(i).Source != user.Item1 && selectQuery.ElementAt(i).UserID != user.Item2 && i<selectQuery.Count());
if (selectQuery.ElementAt(i).Source != user.Item1 && selectQuery.ElementAt(i).UserID != user.Item2) // if true, insert into DB
{
// create new instance of Central
Central c1 = new test1.Central();
// fill up c1 with values
c1.UserID = selectQuery.ElementAt(i).UserID;
c1.Source = selectQuery.ElementAt(i).Source;
c1.DisplayName = selectQuery.ElementAt(i).DisplayName;
c1.Email = selectQuery.ElementAt(i).Email;
c1.Phone = selectQuery.ElementAt(i).Phone;
// insert into db
m_myDb.Centrals.InsertOnSubmit(c1);
m_myDb.SubmitChanges();
} // end if
i = -1;
} // end of foreach