0

通过尝试避免使用许多 foreach()->if() 例程,我尝试了一个 lambda 概念来搜索一堆数据表。在我调试我的代码以查看它不起作用之前,我没有任何错误,因为它不允许向我的数据行列询问它的索引......有没有办法让这个工作而不是使用 IndexOf()?

    static Entity.Produkt ProduktConstructor(DataRow dr)
    {
        Entity.Produkt p = new Entity.Produkt();
        DataTable dt = Entity.KbMirror.mirror.Tables["Produkt"];

        p.id = Guid.Parse(dr[0].ToString());
        p.name = dr[1].ToString();
        byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"];
        p.logo = Converter.ImageConverter.BA2Image(ba);
        foreach (DataRow pvdr in Entity.KbMirror.mirror.Tables["Produkt_Version"].Rows)
            if (Guid.Parse(pvdr[1].ToString()) == p.id)
                p.version.Add(VersionConstructor(Guid.Parse(pvdr[2].ToString() ), Guid.Parse(pvdr[0].ToString() ) ) );
        return p;
    }
    static Entity.Version VersionConstructor(Guid vid, Guid pvid)
    {
        Entity.Version version = new Entity.Version();
        DataTable dt = Entity.KbMirror.mirror.Tables["Version"];

        version.id = vid;
        version.name = dt.Rows[dt.IndexOf(dt.Select().Where(r =>r[0].ToString() == vid.ToString()))][1].ToString();
        foreach (DataRow cvdr in Entity.KbMirror.mirror.Tables["Customer_ProduktVersion"].Rows)
            if (Guid.Parse(cvdr[2].ToString()) == pvid)
                version.customerCollection.Add(CustomerConstructor(Guid.Parse(cvdr[1].ToString())));
        return version;
    }

编辑:

当我像这样使用“IndexOf()”时发生错误:

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

1 回答 1

0

您是否尝试过传递整个表而不是仅传递行?传递整个表格后,您可以仅引用表格的该行。

另外:http: //msdn.microsoft.com/en-us/library/bb552415%28v=vs.110%29.aspx

     // 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"));
     }
于 2013-11-14T17:40:12.907 回答