使用Linq Query查找冲突

时间:2017-01-17 01:55:01

标签: c# linq

我的表格中包含 DateTime 类型的 StartTime 列。如果两行之间的间隔少于10分钟,我应该将两行视为冲突。我想知道的是检查冲突并用linq查询显示它们。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

我可能会这样做:(虽然这是平方时间)

> bc.cal <- generateCalibrationData(bc.bmr, aggregate = FALSE)

Error in generateCalibrationData(bc.bmr, aggregate = FALSE) : 

  unused argument (aggregate = FALSE)

> 

> sessionInfo()

R version 3.2.3 (2015-12-10)

attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:

[1] mlbench_2.1-1     ROCR_1.0-7        gplots_3.0.1      mlr_2.9           
[5] stringi_1.1.1     ParamHelpers_1.10 ggplot2_2.1.0     BBmisc_1.10           

loaded via a namespace (and not attached):

 [1] digest_0.6.9        htmltools_0.3.5     R6_2.2.0            splines_3.2.3          
 [5] scales_0.4.0        assertthat_0.1      grid_3.2.3          stringr_1.0.0          
 [9] bitops_1.0-6        checkmate_1.8.2     gdata_2.17.0        survival_2.38-3        
[13] munsell_0.4.3       tibble_1.2          randomForest_4.6-12 httpuv_1.3.3           
[17] parallelMap_1.3     mime_0.5            DBI_0.5-1           labeling_0.3           
[21] chron_2.3-47        shiny_1.0.0         KernSmooth_2.23-15  plyr_1.8.4             
[25] data.table_1.9.6    magrittr_1.5        reshape2_1.4.1      kernlab_0.9-25         
[29] ggvis_0.4.3         caTools_1.17.1      gtable_0.2.0        colorspace_1.2-6       
[33] tools_3.2.3         parallel_3.2.3      dplyr_0.5.0         xtable_1.8-2           
[37] gtools_3.5.0        backports_1.0.4     Rcpp_0.12.4   

对于n-logn时间,最好先排序然后再进行比较,而LINQ对此有点过分。

答案 1 :(得分:0)

在这种情况下,Linq不是最好的方法。可能创建一个for循环更好。但这是代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("time", typeof(DateTime));

            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 0, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 5, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 20, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 25, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 30, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 40, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 45, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 50, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 56, 0) });
            dt.Rows.Add(new object[] { new DateTime(2017, 1, 1, 1, 58, 0) });

            List<DateTime> times = dt.AsEnumerable().Where((x, i) => (i > 0) && (x.Field<DateTime>("time") - dt.Rows[i - 1].Field<DateTime>("time")).TotalMinutes < 10).Select(x => x.Field<DateTime>("time")).ToList();

        }
    }
}