pal*_*hta 7 .net c# sql linq datatable
我有两列数据表FromDate和ToDate,这是字符串格式.我想检查我的table.ie中是否有任何重复记录
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
Run Code Online (Sandbox Code Playgroud)
该表包含其日期范围映射的重复记录
From Date To Date
----------------------
9/01/2012 9/16/2012
9/15/2012 9/23/2012
Run Code Online (Sandbox Code Playgroud)
它应该返回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();
Run Code Online (Sandbox Code Playgroud)