[英]Select and extract values from DataRows in DataTable - C#
I have a method that accept these arguments: 我有一个接受这些参数的方法:
public bool CalculateProvent(ArrayList UPs,
ArrayList DateExtraction,
DataTable SourceData
{
...
...
}
The SourceData datatable has this content (for example): SourceData数据表具有以下内容(例如):
UP - DateExtraction - Energy1 - Energy2 - Consumption1 - Consumption2 - etc
with some values: 具有一些值:
780 - 2011-11-12 14:00:00 - 30.50 - NULL - NULL - 12.00
780 - 2011-11-12 15:00:00 - 31.50 - NULL - 10 - 12.00
781 - 2011-11-12 14:00:00 - NULL - 6 - NULL - 12.00
...
I have to iterate this datatable based on the UPs and DataExtraction arraylist's to extract the needed field values (Energy1, etc.). 我必须基于UP和DataExtraction数组列表来迭代此数据表,以提取所需的字段值(Energy1等)。
For example: 例如:
Decimal myEnergy1, myEnergy2, myConsumption1, myConsumption2
foreach(int up in UPs)
{
foreach(DateTime myTime in DateExtraction)
{
// Here I have to select the correct row and extract my fields
}
}
How can I select the correct values in this iteration? 如何在此迭代中选择正确的值?
I think something like this might be what you are looking for. 我认为您可能正在寻找类似这样的东西。
var q = from up in UPs.Cast<int>()
from myTime in DateExtraction.Cast<DateTime>()
join r in SourceData.AsEnumerable() on
new { r.Field<int>("UP"), r.Field<DateTime>("DateExtraction")} equals
new { up, myTime }
select new
{
Energy1 = r.Field<Decimal>("Energy1"),
Energy2 = r.Field<Decimal>("Energy2")
};
try this: 尝试这个:
foreach(int up in UPs)
{
foreach(DateTime myTime in DateExtraction)
{
DataRow[] myRows = SourceData.Select("UP=" + up + " AND DateExtraction='" + myTime.ToString() + "'");
if(myRows != null && myRows.Length == 1)
{
// here is your row... myRows[0]
}
}
}
of course should be improved and pay attention to the myTime.ToString() format. 当然应该改进并注意myTime.ToString()格式。 surely this can also be done with nice and compact LINQ to DataSets
当然,这也可以使用美观而紧凑的LINQ to DataSets完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.