从DataRow []获取DateTime值并将其传递给var

时间:2021-05-26 10:20:34

I wish to insert a Date value in DataRow[] and find a DateTime value in a column. The Date value entered will be passed to a var to perform another search. But it cannot be passed, can any1 help me fix my code?

我希望在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();      

1 个解决方案

#1


1  

Once you filtered rows by date-time you need to apply Select on that bunch of rows. For example,

按日期时间过滤行后,您需要在该行行上应用Select。例如,

var PcrID_1 = setStartDateTime.Select(d => d["PcrID"]);
Pcr_1 = PcrID_1.Count();      

Of course , above code doesn't make sense if all you want is the count then you can very well do that by saying

当然,上面的代码没有意义,如果你想要的只是计数,那么你可以通过说,你可以做到这一点

Pcr_1 = setStartDateTime.length;

Or you can do the original filtering using LINQ - for example,

或者你可以使用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"]);

The first and last example code actual produces enumeration of PcrID for the given date.
If you want a distinct list then you can use Distinct extension method - for example

第一个和最后一个示例代码实际生成给定日期的PcrID枚举。如果你想要一个不同的列表,那么你可以使用Distinct扩展方法 - 例如

 var ids = DataSet1.Tables["table1"].AsEnumerable()
                      .Where(c => c.Field<DateTime>("StartDateTime") == searchDate)
                      .Select(d => d["PcrID"]).Distinct();

#1


1  

Once you filtered rows by date-time you need to apply Select on that bunch of rows. For example,

按日期时间过滤行后,您需要在该行行上应用Select。例如,

var PcrID_1 = setStartDateTime.Select(d => d["PcrID"]);
Pcr_1 = PcrID_1.Count();      

Of course , above code doesn't make sense if all you want is the count then you can very well do that by saying

当然,上面的代码没有意义,如果你想要的只是计数,那么你可以通过说,你可以做到这一点

Pcr_1 = setStartDateTime.length;

Or you can do the original filtering using LINQ - for example,

或者你可以使用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"]);

The first and last example code actual produces enumeration of PcrID for the given date.
If you want a distinct list then you can use Distinct extension method - for example

第一个和最后一个示例代码实际生成给定日期的PcrID枚举。如果你想要一个不同的列表,那么你可以使用Distinct扩展方法 - 例如

 var ids = DataSet1.Tables["table1"].AsEnumerable()
                      .Where(c => c.Field<DateTime>("StartDateTime") == searchDate)
                      .Select(d => d["PcrID"]).Distinct();