将嵌套循环转换为LINQ [英] Converting nested loops into LINQ

查看:86
本文介绍了将嵌套循环转换为LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有DateTime和一个DateTimes数组的属性.

I have a property that has a DateTime and an array of DateTimes.

 public DateTime date { get; set; }
 public DateTime[] dateArray { get; set; }

当前,我正在使用嵌套循环来获取我的值,但是我试图将其更改为LINQ并且无法正确获取.

Currently I am using nested loops to get my values, but I am trying to change this to LINQ and cannot get it correct.

 for (var i = 0; i < allPropertyValues.Count(); i ++)
 {
      var list = new List<DateTime>();
      for (var j = 0; j < allPropertyValues[i].dateArray.Count(); j++)
      {
           if (allPropertyValues[i].dateArray[j]
                 >= allPropertyValues[i].date)
                list.Add(allPropertyValues[i].dateArray);
      }
 }

allPropertyValues变量当前是静态属性的数组,并且在每个索引中,有一个date值和多个dateArray值.如何使用LINQ遍历每个属性结果并比较每个日期与获得的dateArray的多个值?非常感谢.

The allPropertyValues variable is currently an array of static properties, and in each index, there is one date value and multiple dateArray values. How do I iterate through each property result and compare each date, with the multiple values of the dateArray that I get, using LINQ? Thanks very much in advance.

此外,我将如何比较这两个实例,因为我似乎无法在LINQ表达式中使用><.我没有使用LINQ太多,但是正设法弄清楚它,再次感谢.

Also, how would I do a compare of the two instances, as it doesn't appear that I can use > or < in a LINQ expression. I haven't used LINQ too much but am trying to figure it out properly, thanks again.

推荐答案

请遵循以下准则:

  • 从内而外地工作
  • 将常见的子表达式提取为更简单的
  • 问一下循环所代表的概念是什么,而不是循环 的含义.然后编写一个代表该概念的查询.
  • Work your way from inside to outside
  • Extract common subexpressions into something simpler
  • Ask yourself what the concept is that the loop represents, rather than what the loop does. Then write a query that represents that concept.

好.我们有内部循环,这是一团糟.我已经修复了评论中提到的明显错误.

OK. We have our inner loop, which is a mess. I've fixed the obvious error mentioned in a comment.

  for (var j = 0; j < allPropertyValues[i].dateArray.Count(); j++)
  {
    if (allPropertyValues[i].dateArray[j] >= allPropertyValues[i].date)
      list.Add(allPropertyValues[i].dateArray[j]);
  }

为什么一团糟?好吧,我们在整个演出中都有一个共同的子表达式.简化它.

Why it is a mess? Well, we have a common subexpression all over the show here. Simplify it.

  var v = allPropertyValues[i];
  for (var j = 0; j < v.dateArray.Count(); j++)
  {
    if (v.dateArray[j] >= v.date)
      list.Add(v.dateArray[j]);
  }

这已经比阅读容易一百万倍.我们可以使它更容易吗?是的.我们有第二个通用子表达式:

Already this is one million times easier to read. Can we make it even easier? Yes. We have a second common subexpression:

  var v = allPropertyValues[i];
  for (var j = 0; j < v.dateArray.Count(); j++)
  {
    DateTime d = v.dateArray[j];
    if (d >= v.date)
      list.Add(d);
  }

现在我们意识到j是不必要的:

And now we realize that j is unnecessary:

  var v = allPropertyValues[i];
  foreach (DateTime d in v.dateArray)
  {
    if (d >= v.date)
      list.Add(d);
  }

OMG非常容易理解.现在,很清楚代码的含义是什么,因此我们可以更轻松地将其转换为查询:

OMG so much easier to understand. Now it becomes clear what the meaning of the code is, and so we can much more easily turn it into a query:

  var v = allPropertyValues[i];
  list.AddRange(from d in v.dateArray where d >= v.date select d);

  var v = allPropertyValues[i];
  list.AddRange(v.dateArray.Where(d => d >= v.date));

现在我们只剩下两行极其清晰的代码.

And now we're down to two lines of extremely clear code.

现在,我们重写了内部循环.将其放在外部循环中:

Now we've rewritten our inner loop. Put it in the outer loop:

for (var i = 0; i < allPropertyValues.Count(); i++)
{
  var list = new List<DateTime>();
  var v = allPropertyValues[i];
  list.AddRange(v.dateArray.Where(d => d >= v.date));
}

现在,再次检查代码.可以简化吗?是的.我们注意到列表的创建早于必要.

Now again, examine the code. Can it be simplified? Yes. We notice that the list is being created earlier than necessary.

for (var i = 0; i < allPropertyValues.Count(); i++)
{
  var v = allPropertyValues[i];
  var list = v.dateArray.Where(d => d >= v.date).ToList();
}

我们还能简化这个吗?当然.现在我们可以注意到i是不必要的:

Can we make this simpler still? Sure. Now we can notice that i is unnecessary:

foreach (var v in allPropertyValues)
{
  var list = v.dateArray.Where(d => d >= v.date).ToList();
}

现在我们将整个过程简化为两行程序片段.

And now we've reduced the whole thing to a two-line program fragment.

如果您想要的是列表列表,那么我们可以走的更远:

If what you want is a list of lists then we can go even further:

List<List<DateTime>> lists = allPropertyValues
  .Select(v => v.dateArray.Where(d => d >= v.date).ToList())
  .ToList();

我们完成了.

这里的重点是:不断地问自己,如何简化程序以使其更加清晰简洁..然后应用一系列仔细,小而正确的重构.每次您这样做时,请再问一遍:现在我可以做得更好了吗?

The takeaway here is: constantly be asking yourself how you can simplify your program to make it more clear and concise. Then apply a series of careful, small, correct refactorings. Every time you do, ask yourself again: can I now make it even better?

这篇关于将嵌套循环转换为LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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