我希望在DataRow []中插入Date值,并在列中查找DateTime值。输入的日期值将传递给var以执行另一次搜索。但它无法通过,任何1可以帮我修复我的代码吗?
//Receive StartDateTime value
DataRow[] setStartDateTime = DataSet1
.Tables["table1"]
.Select("StartDateTime =#" + searchMonth + "/" + searchDay + "/" + searchYear + "#");
//Count PcrID rows which same row with StartDateTime
var PcrID_1 = DataSet1
.Tables["table1"]
.AsEnumerable()
.Where(c => c.Field<DateTime>("StartDateTime") == setStartDateTime) /*Error occurs here: Operator '==' cannot be applied to operands of type 'System.DateTime' and 'System.Data.DataRow[]'*/
.Select(d => d["PcrID"]);
Pcr_1 = PcrID_1.Count();
答案 0 :(得分:1)
按日期时间过滤行后,您需要在该行行上应用Select
。例如,
var PcrID_1 = setStartDateTime.Select(d => d["PcrID"]);
Pcr_1 = PcrID_1.Count();
当然,上面的代码没有意义,如果你想要的只是计数,那么你可以通过说
来做到这一点。Pcr_1 = setStartDateTime.length;
或者您可以使用LINQ进行原始过滤 - 例如,
var searchDate = new DateTime(searchYear, searchMonth, searchDay);
var PcrID_1 = DataSet1.Tables["table1"].AsEnumerable()
.Where(c => c.Field<DateTime>("StartDateTime") == searchDate)
.Select(d => d["PcrID"]);
第一个和最后一个示例代码实际生成给定日期的PcrID枚举 如果您想要一个不同的列表,那么您可以使用Distinct扩展方法 - 例如
var ids = DataSet1.Tables["table1"].AsEnumerable()
.Where(c => c.Field<DateTime>("StartDateTime") == searchDate)
.Select(d => d["PcrID"]).Distinct();