C# 如何从数据集中读取特定行

C# 如何从数据集中读取特定行,c#,C#,这是我创建表的代码。这段代码返回所有行,但我只想要项目价格大于6的特定行 class Program { static void Main(string[] args) { DataTable MyTable = GetTable(); // Get the data table. foreach (DataRow row in MyTable.Rows) // Loop over the rows. {

这是我创建表的代码。这段代码返回所有行,但我只想要项目价格大于6的特定行

class Program
{
    static void Main(string[] args)
    {
        DataTable MyTable = GetTable(); // Get the data table.

        foreach (DataRow row in MyTable.Rows) // Loop over the rows.
        {
            Console.WriteLine("--- Row ---");
            foreach (var item in row.ItemArray) // Loop over the items.
            {
                Console.Write("Item: "); 
                Console.WriteLine(item);
            }
        }

        Console.Read(); // Pause.
    }

    static DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Price", typeof(int));
        table.Columns.Add("Name", typeof(string));

        table.Rows.Add(15, "Bag");
        table.Rows.Add(4, "Candies");
        table.Rows.Add(2, "Cookies");
        table.Rows.Add(20, "Books");
        table.Rows.Add(8, "Chocolates");
        return table;
    }
  }
}

在展示之前,只需检查价格,如下所示:

foreach(MyTable.Rows中的DataRow)//在行上循环。
{
Console.WriteLine(“----行---”;
如果(第[“价格”]>6行)
{
foreach(row.itemrarray中的var item)//在项目上循环。
{
控制台。写入(“项:”);
控制台写入线(项目);
}
}

}
尝试时会发生什么?它会引发异常还是什么?它只是生成所有输出,不显示您插入的“if(row[“Price”]>6)”所示的特定行?