C# 如何通过特定数据行的列值获取其索引

C# 如何通过特定数据行的列值获取其索引,c#,.net,visual-studio-2010,visual-studio,datatable,C#,.net,Visual Studio 2010,Visual Studio,Datatable,通过尝试避免使用许多foreach()->if()例程,我尝试使用lambda概念来搜索一组数据表。我没有错误,直到我调试我的代码,看到它不工作,因为它不允许要求我的datarow列的索引。。。有没有什么方法可以代替使用IndexOf()来实现这一点 编辑: 使用“IndexOf()”时出错,如下所示: byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString

通过尝试避免使用许多foreach()->if()例程,我尝试使用lambda概念来搜索一组数据表。我没有错误,直到我调试我的代码,看到它不工作,因为它不允许要求我的datarow列的索引。。。有没有什么方法可以代替使用IndexOf()来实现这一点

编辑:

使用“IndexOf()”时出错,如下所示:

        byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"];

你有没有试着把整张桌子都传过去,而不是只传那一排?传递整个表后,就可以只引用表中的那一行

此外:

//填充数据集。
数据集ds=新数据集();
ds.Locale=CultureInfo.InvariantCulture;
数据集(ds);
DataTable products=ds.Tables[“产品”];
IEnumerable查询=
来自products.AsEnumerable()中的product
选择产品;
Console.WriteLine(“产品名称:”);
foreach(查询中的数据行p)
{
Console.WriteLine(p.Field(“Name”));
}

我建议使用“LINQ到数据集”。这不是我真正想要的。实际上,我只想通过特殊列中的任何值查询datatable中的行索引。
        byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"];
     // Fill the DataSet.
     DataSet ds = new DataSet();
     ds.Locale = CultureInfo.InvariantCulture;
     FillDataSet(ds);

     DataTable products = ds.Tables["Product"];

     IEnumerable<DataRow> query =
         from product in products.AsEnumerable()
         select product;

     Console.WriteLine("Product Names:");
     foreach (DataRow p in query)
     {
         Console.WriteLine(p.Field<string>("Name"));
     }