LINQ合并两个列表并按时间戳分组 [英] LINQ merging two Lists and grouping by timestamps

查看:108
本文介绍了LINQ合并两个列表并按时间戳分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,我有两个列表需要合并在一起并从中创建数据表,我有以下代码块:

Good morning, I've got two lists that I need to merge together and create a data table from, I've got the following block of code:

private static DataTable GetDataTable(IList<DataValue> listOneDataValues,
                                            IList<DataValue> listTwoDataValues)
      {
         var dataTable = new DataTable();
         dataTable.Columns.Add("ColumnFromListOne");
         dataTable.Columns.Add("ColumnFromListTwo");
         dataTable.Columns.Add("TimeStamp");

         //Group the lists together
         var query = (from listOne in listOneDataValues
                      from listTwo in listTwoDataValues
                      let columnFromListOne= listOne.DoubleValue
                      let columnFromListTwo= listTwo.DoubleValue
                      let timestamp = listOne.TimeStamp
                      where listOne.TimeStamp == listTwo.TimeStamp
                      select new {ColumnFromListTwo = columnFromListOne, ColumnFromListOne = columnFromListTwo, Timestamp = timestamp});


         foreach(var q in query)
            dataTable.Rows.Add(q.ColumnFromListOne, q.ColumnFromListTwo, q.TimeStamp);

         return dataTable;
      }

问题是两个列表包含的时间戳大约相差几秒钟,而且它们根本不对齐,因此即使每个列表包含200个时间戳,我的最终结果还是在数据表中记录了一个或零个记录+记录.我对LINQ感到很不满意,希望能向正确的方向发展.我想我需要在分组之前对时间戳进行插值,但是我想知道执行此类操作的最佳实践模式.

The problem is the two lists contain timestamps that are off by a matter of seconds, and they don't align at all, so my end result ends up with one or zero records in the datatable, even though each list contains 200+ records. I'm pretty bad with LINQ and would appreciate a point in the right direction. I guess I need to interpolate the timestamps before grouping, but I would like to know the best practice pattern for doing something like this.

推荐答案

您需要确定将两个时间戳称为相等"的适当阈值-知道阈值太大会给您带来误报,而对于阈值来说会太小阈值将阻止某些记录的加入.

You need to decide what an appropriate threshold is for calling two time stamps "equal" - knowing that too large a threshold will give you false positives and too small a threshold will prevent some records from joining.

从那里只需将查询更改为

From there just change your query to

     int threshold = 5;
     //Group the lists together
     var query = (from listOne in listOneDataValues
                  from listTwo in listTwoDataValues
                  where Math.Abs(
                                    (listOne.TimeStamp - listTwo.TimeStamp)
                                     .TotalSeconds
                                ) <= threshold
                  select new {
                                 ColumnFromListTwo = listTwo.DoubleValue,
                                 ColumnFromListOne = listOne.DoubleValue, 
                                 Timestamp = listOne.TimeStamp
                             });

这篇关于LINQ合并两个列表并按时间戳分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆