从IEnumerable中删除子IEnumerable

时间:2015-09-15 12:08:05

标签: c# linq ienumerable

我有一个<div class="col col-50" ng-if="categories.data.indexOf(categories.data[$index+1])!=-1"> <div class="list card" ng-click="getProducts(categories.data[$index + 1].category_id,categories.data[$index + 1].category_name)"> <div class="item item-image"> <img src="{{categories.image_path}}{{categories.data[$index + 1].category_image}}" style="min-height:250px;max-height:250px;" /> </div> <ion-item class="item" style="text-align:center">{{categories.data[$index + 1].category_name}}</ion-item> </div> </div> ,我想删除一个只能出现一次的字节序列。第一个数组可能很大,但第二个数组不能超过50字节

这样做的最佳方式(也是最快的方式)是什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您可以为此创建静态方法,如下所示:

public static class Helper
{
    public static IEnumerable<byte> RemoveSubSequence(this IEnumerable<Byte> sequence, IEnumerable<Byte> subSequence)
    {
        List<byte> list = sequence.ToList();
        byte[] subSequenceList = subSequence.ToArray();

        int i = 0;
        int count = 0;

        for (; i < list.Count && count != subSequenceList.Length; i++)
            for (int i2 = 0; i2 < subSequenceList.Length && count != subSequenceList.Length; i2++)
                if (list[i + i2] == subSequenceList[i2]) count++; else count = 0;

        list.RemoveRange(i - 1, count);

        return list;
    }
}

然后你就可以使用它:

        IEnumerable<byte> bytes = new byte[] { 5, 7, 6, 9, 1, 5, 7, 6, 7, 0, 6, 4, 0, 6, 4, 8 };
        IEnumerable<byte> subSequence = new byte[] { 6, 7, 0, 6, 4, 0, 6, 4, 8 };

        bytes = bytes.RemoveSubSequence(subSequence);

        foreach (var item in bytes) Console.Write(item + "  ");

        Console.WriteLine("\n\n");

输出:5, 7, 6, 9, 1, 5, 7,

答案 1 :(得分:0)

IEnumerable是readonly,如果你想删除它的一些居民,你应该把它称为列表而不是&#34;选择... bla.ToList()&#34;