Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用LINQ对数据表进行排序_Linq_C# 3.0 - Fatal编程技术网

使用LINQ对数据表进行排序

使用LINQ对数据表进行排序,linq,c#-3.0,Linq,C# 3.0,我有一个DataTable,它的结构如下。 StartDate(类型DateTime) EndDate(键入DateTime) 说明(键入文本) 我需要检查的是日期是否重叠,即1个项目的开始日期是否小于前一个项目的结束日期 这是我写的代码 注意:我已经进行了检查,以确保每个记录的开始日期必须小于结束日期 dtDates.DefaultView.Sort = "StartDate ASC"; //Sort the DataTable based on the Start Dates if(dtDa

我有一个DataTable,它的结构如下。 StartDate(类型DateTime) EndDate(键入DateTime) 说明(键入文本)

我需要检查的是日期是否重叠,即1个项目的开始日期是否小于前一个项目的结束日期

这是我写的代码

注意:我已经进行了检查,以确保每个记录的开始日期必须小于结束日期

dtDates.DefaultView.Sort = "StartDate ASC"; //Sort the DataTable based on the Start Dates
if(dtDates.Rows.Count > 1) //Check only if more than 1 Date is specified
{
    for (int i = 1; i < dtDates.Rows.Count; i++)
    {
        if(TypeConvert.ToDate(dtDates.Rows[i]["StartDate"] < TypeConvert.ToDate(dtDates.Rows[i-1]["EndDate"])
            {
                retVal = true;
                currentRow = i;
                break;
            }
     }
}
dtDates.DefaultView.Sort=“StartDate ASC”//根据开始日期对数据表进行排序
if(dtDates.Rows.Count>1)//仅当指定了多个日期时才检查
{
对于(int i=1;i
上面的代码运行良好

但是现在,我想用LINQ实现它

下面是我试图尝试的代码

如何使用LINQ进行整个比较

    public static bool CheckIfDatesOverlap(DataTable dataTable)
    {
        bool retVal = false;
        int currentRow = 0;
        List<DataRow> listDates = (from DataRow dgv in dataTable.Rows
                                               select dgv).OrderBy(x => x[StartDate]).ToList();
        if (listDates.Count > 1) //Perform Comparision only if more than 1 row is present
        {
            for (int i = 1; i < listDates.Count; i++)
            {
                if (TypeConvert.ToDateTime(listDates[i][StartDate]) < TypeConvert.ToDateTime(listDates[i][EndDate]))
                {
                    retVal = true; //There are duplicates, hence return true
                    currentRow = i;
                    break;
                }
                else
                {
                    //Do nothing as dates do not overlap
                }
            }
        }
        else
        {
            retVal = false; //As there is only 1 row, return false
        }
        if (retVal)
        {
            string message = "Dates Overlap";
            //Display message
        }
        return retVal;
    }
publicstaticboolcheckifdatesoverlap(DataTable)
{
bool-retVal=false;
int currentRow=0;
List listDates=(来自dataTable.Rows中的DataRow dgv
选择dgv).OrderBy(x=>x[StartDate]).ToList();
if(listDates.Count>1)//仅当存在多行时执行比较
{
对于(int i=1;i
如果使用selectwith previous description,则可以编写以下内容:

bool datesOverlap = table.Rows
    .Cast<DataRow>()
    .SelectWithPrevious((current, previous) => new
        {
            PreviousEndDate = (DateTime)previous["EndDate"],
            StartDate = (DateTime)current["StartDate"]
        })
    .Any(x => x.StartDate < x.PreviousEndDate);
bool datesOverlap=table.Rows
.Cast()
。选择WithPrevious((当前、以前)=>new
{
PreviousEndDate=(DateTime)previous[“EndDate”],
StartDate=(日期时间)当前[“StartDate”]
})
.Any(x=>x.StartDate
如果使用selectwith previous description,则可以编写以下内容:

bool datesOverlap = table.Rows
    .Cast<DataRow>()
    .SelectWithPrevious((current, previous) => new
        {
            PreviousEndDate = (DateTime)previous["EndDate"],
            StartDate = (DateTime)current["StartDate"]
        })
    .Any(x => x.StartDate < x.PreviousEndDate);
bool datesOverlap=table.Rows
.Cast()
。选择WithPrevious((当前、以前)=>new
{
PreviousEndDate=(DateTime)previous[“EndDate”],
StartDate=(日期时间)当前[“StartDate”]
})
.Any(x=>x.StartDate
Jon Skeet再次罢工:)谢谢你指出这一点,这对我帮助很大,我猜这就是OP想要的答案。Jon Skeet再次罢工:)谢谢你指出这一点,这对我帮助很大,我猜这就是OP想要的答案。