LINQ:如何使用removeall过不使用对于数组循环 [英] LINQ: How to Use RemoveAll without using For loop with Array

查看:158
本文介绍了LINQ:如何使用removeall过不使用对于数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个日志对象,我想从中删除对象,基于LINQ查询。我想删除日志中的所有记录,如果一个程序内的版本的总和大于60.目前我很相信这将工作,但似乎kludgy:

I currently have a log object I'd like to remove objects from, based on a LINQ query. I would like to remove all records in the log if the sum of the versions within a program are greater than 60. Currently I'm pretty confident that this'll work, but it seems kludgy:

        for (int index = 0; index < 4; index++)
        {
          Log.RemoveAll(log =>
                    (log.Program[index].Version[0].Value +
                     log.Program[index].Version[1].Value +
                     log.Program[index].Version[2].Value ) > 60);
        }

程序是一个由4个值组成的数组,版本有3个数组中。有没有更简单的方法来做这个RemoveAll在LINQ不使用for循环?

The Program is an array of 4 values and version has an array of 3 values. Is there a more simple way to do this RemoveAll in LINQ without using the for loop?

感谢您提供任何协助!

编辑:
不幸的是,Program和Version基于的变量类型(这是我工作的框架的约束)限制我们,使我不能访问任何成员中。但我确认tzaman的解决方案的工作原理,如果你有列表通过创建一些示例代码。

Unfortunately the type of variable that Program and Version are based off of (which is a constraint of the framework I'm working in) restricts us such that I cannot access the "Any" member. I however confirmed that tzaman's solution works if you have lists by creating some sample code. I'm restricted to array-like variables (see the commented out areas)

// I'm restricted to Arrays, but if I had lists, this would work. 
  internal class MyLogCollection
  {
    List<MyLog> MyListOfZones = new List<MyLog>();

    public void TestRemove()
    {
      // Original Implementation
      for (int i = 0; i < 4; i++)
      {
        MyListOfZones.RemoveAll(log => (log.MyZoneArray[0].MyVersionArray[0].Value +
                                        log.MyZoneArray[0].MyVersionArray[1].Value +
                                        log.MyZoneArray[0].MyVersionArray[2].Value) > 60);
        //"Any" method is not available off of intellisense scope on MyZoneArray 
      }

      // Better Implementation (thanks tzaman!)
      MyListOfZones.RemoveAll(log => (log.MyZoneArray.Any(prog =>
                                                          prog.MyVersionArray.Sum(ver => ver.Value) > 60)));
    }
  }
  internal class MyLog
  {
    //public MyZone[] MyZoneArray = new MyZone[4];

    public List<MyZone> MyZoneArray = new List<MyZone>(4);

  }
  internal class MyZone
  {
    //public MyVersion[] MyVersionArray = new MyVersion[3];
    public List<MyVersion> MyVersionArray = new List<MyVersion>(3);
  }
  internal class MyVersion
  {
    public byte Value { get; set;}

  }

感谢tzaman!

推荐答案

我应该这样做:

Log.RemoveAll(log => 
              log.Program.Any(prog => 
                              prog.Version.Sum(ver => ver.Value) > 60));



EDIT:添加扩展方法以从可索引的数组类对象中获取 IEnumerable ,以便您可以在它们上使用LINQ:


Okay, so here's how to add extension methods to get IEnumerables from your indexable "array-like" objects, so that you can use LINQ on them:

static class MyExtensions
{
    public static IEnumerable<MyZone> Enumerate(this MyZoneArray zone)
    {
        for (int i = 0; i < zone.Length; i++)
            yield return zone[i];
    }

    public static IEnumerable<MyVersion> Enumerate(this MyVersionArray version)
    {
        for (int i = 0; i < version.Length; i++)
            yield return version[i]
    }
}

我假设 MyZoneArray MyVersionArray 类型有一个长度字段,但如果不是你可以放入 code>和 3 。通过这些,您现在可以调用集合对象上的 Enumerate()函数,以获取 IEnumerable 版本该集合与所有相关的LINQy良好附加: log.MyZoneArray.Enumerate()。Any(...)

I'm assuming the MyZoneArray and MyVersionArray types have a Length field, but if not you could just put in 4 and 3 over there. With these in place, you can now call the Enumerate() function on the collection object to get an IEnumerable version of the collection, with all the associated LINQy goodness attached: log.MyZoneArray.Enumerate().Any( ... )

这篇关于LINQ:如何使用removeall过不使用对于数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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