通过尝试避免许多foreach() - > if()例程,我尝试了一个lambda概念来搜索一堆DataTables。我没有错误,直到我调试我的代码看它不起作用,因为它不允许向我的datarow-column询问其索引...有没有办法让这个工作而不是使用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"];
答案 0 :(得分: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"));
}