'的foreach'对表中的所有行执行操作

时间:2014-06-29 01:56:25

标签: windows-phone-8 foreach sql-server-ce

我有以下课程& DataContext结构,

public class Accounts
{
    public string AccountName { get; set; }

    public string SecretKey { get; set; }
}

class MyDataContext : DataContext
{
    public MyDataContext (string ConnectionString) : base(ConnectionString)
    {

    }

    public Table<Accounts> AllAccounts
    {
        get
        {
            return this.GetTable<Accounts>();
        }
    }
}

我想从“帐户表”中获取所有帐户,并使用他们的每个SecretKeys获取PIN并在列表框中显示所有这些帐户...

问题是我无法创建foreach语句

类似

foreach (AllAccountsAC in MyDataContext.Accounts)

1 个答案:

答案 0 :(得分:0)

首先,您无法在不键入变量的情况下执行foreach循环。对于Table,您必须在其行上执行foreach

Table t = new Table();
foreach (TableRow tr in t.Rows)
{
    //enter code here
}

获得行后,您可以获取数据。

其次,您无法输入Table。您可以使用键入的List(例如List<Accounts>作为您的类)并执行相同操作,但使用List中设置的类型:

List<Accounts> AccountList = new List<Accounts>();
foreach (Accounts a in AccountList)
{
    //enter code here
}

从列表中获得对象后,您可以在其上执行所需的任何功能。