Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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
C# 如何使两个列表中的记录数相等,以便在c中进行计算#_C#_.net - Fatal编程技术网

C# 如何使两个列表中的记录数相等,以便在c中进行计算#

C# 如何使两个列表中的记录数相等,以便在c中进行计算#,c#,.net,C#,.net,我有一个等式,比如说p1+p2。我用两个列表来计算这个等式 List<P1> p_one and List<P2> p_two; 第一个结果是同时添加第一个结果。第二个结果是p_one的值=3,日期=2018-09-18 10:05:00和值=3,日期=2018-09-18 10:04:30之和。结果是6,依此类推。因此,在输出列表中,最大记录数将为8(例如)。您可以使用如下边距检查是否相等: public static bool IsEqual(DateTime d1

我有一个等式,比如说
p1+p2
。我用两个列表来计算这个等式

List<P1> p_one and List<P2> p_two;

第一个结果是同时添加第一个结果。第二个结果是p_one的值=3,日期=2018-09-18 10:05:00和值=3,日期=2018-09-18 10:04:30之和。结果是6,依此类推。因此,在输出列表中,最大记录数将为8(例如)。

您可以使用如下边距检查是否相等:

public static bool IsEqual(DateTime d1, DateTime d2, int marginSeconds = 30)
{
    return Math.Abs((d1 - d2).Seconds) < marginSeconds;
}
publicstaticboolsequal(DateTime d1,DateTime d2,int-marginSeconds=30)
{
返回Math.Abs((d1-d2).Seconds)
如果d1为“2018-09-18 10:15:00”d2为“2018-09-18 10:15:10”,则应返回
true

然后在你的循环中使用这个来均衡


希望这有帮助。

您可以使用循环通过
p\u one
,同时在每个步骤中应用LINQ方法,从
p\u two
当前
p\u one
记录的“30秒距离”获取所有记录。 然后,您可以将其存储在
元组的
列表中,例如,以获取所有信息

List<RESULTSET> summary = new List<RESULTSET>();
for (int i = 0; i < p_one.Count; i++)
    summary.AddRange(p_two.Where(p2 => Math.Abs((p2.DateTime - p_one[i].DateTime).TotalSeconds) <= 30).Select(p2 => new RESULTSET(){Value = p_one[i].Value + p2.Value, DateTime = p_one[i].DateTime }));

请注意,它们是相同的,您可以在任何地方使用
P1
类。

这只是一个例子,我没有考虑这里的数据类型。均衡(仅保留在一个位置、合并条目或其他什么)是什么意思?您的均衡标准是什么(10秒、20秒等的差异)?我会更新我的问题。你不能提供真实的数据和你的班级吗?例如,
DateTime
应该是
DateTime
而不是
字符串。提供伪代码很少是一个好主意。原始结果集非常大,我只是使用了两个字段来理解它。DateTime是DateTime类型这不是我期望的。我测试了method@NithinMohan尝试更新答案。这里的
Tuple
用两个字段表示类:一个
int
和一个
DateTime
。你可以用自己想要的类来代替它。我更新了问题。未达到预期结果。对于您的回答,它将返回67条记录,但我的预期结果最多包含8条记录,这相当于较小列表p_one的记录数。
public static bool IsEqual(DateTime d1, DateTime d2, int marginSeconds = 30)
{
    return Math.Abs((d1 - d2).Seconds) < marginSeconds;
}
List<RESULTSET> summary = new List<RESULTSET>();
for (int i = 0; i < p_one.Count; i++)
    summary.AddRange(p_two.Where(p2 => Math.Abs((p2.DateTime - p_one[i].DateTime).TotalSeconds) <= 30).Select(p2 => new RESULTSET(){Value = p_one[i].Value + p2.Value, DateTime = p_one[i].DateTime }));
public class P1 { public int Value; public DateTime DateTime; }
public class P2 { public int Value; public DateTime DateTime; }
public class RESULTSET { public int Value; public DateTime DateTime; }