c# – 检查表是否包含重叠的时间跨度

前端之家收集整理的这篇文章主要介绍了c# – 检查表是否包含重叠的时间跨度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有两列FromDate和ToDate的数据表,它们是字符串格式.
我想检查一下我的table.i.e中是否有任何重复的记录
From Date    To Date
----------------------      
9/01/2012    9/16/2012   
8/23/2012    8/24/2012   
8/25/2012    8/25/2012   
8/5/2012     8/6/2012    
8/26/2012    8/27/2012   
9/15/2012    9/23/2012

该表包含重复记录,因为它们的日期范围是映射的

From Date       To Date      
----------------------      
9/01/2012    9/16/2012   
9/15/2012    9/23/2012

它应该返回false.

解决方法

var query = from row in dt.AsEnumerable()
            from row1 in dt.AsEnumerable()
            where
            (

                 (
                     DateTime.Parse(row1.Field<string>("fromDate")) >= DateTime.Parse(row.Field<string>("fromDate")) &&
                     DateTime.Parse(row1.Field<string>("fromDate")) <= DateTime.Parse(row.Field<string>("toDate"))
                 )
                 ||
                 (
                     DateTime.Parse(row1.Field<string>("toDate")) >= DateTime.Parse(row.Field<string>("fromDate")) &&
                     DateTime.Parse(row1.Field<string>("toDate")) <= DateTime.Parse(row.Field<string>("toDate"))
                 )
            )
            select new
            {
                fromDate = DateTime.Parse(row1.Field<string>("fromDate")),toDate = DateTime.Parse(row1.Field<string>("toDate"))
            };
//This lst contains the dates which are overlapping    
var lst = query.Distinct().ToList();
原文链接:https://www.f2er.com/csharp/99080.html

猜你在找的C#相关文章