循环遍历可观察集合以创建新的可观察集合

时间:2014-08-05 13:14:22

标签: c# linq observablecollection skip-take

大家好我有一个可观察的集合,我想要的是创建一个新的可观察集合,它将获取原始集合的前6个对象并跳过下一个12但是在循环中所以需要6跳过12乘6跳过12只要那里有物体。

我已经阅读了关于take和skip方法的内容,并且使用过它们但效果不大。如果我说6,它会占用前6,然后在没有循环的情况下停止,如果我执行6,跳过12它将永远不会进入循环只是跳过它等等。希望你们在这里可以提供一些帮助。

 private void UcitajReport()
    {
        _report = new ObservableCollection<ReportClass>();


        foreach (Brojevi b in SviBrojevi.Skip(0).Take(6))


        {


            ReportClass r = new ReportClass();

            r.RpBrId = b.ID;
            r.RpBrBroj = b.Broj;
            r.RpBrKolo = b.Kolo;

            r.RpKlKolo = (from ko in Kola
                        where ko.ID == b.KoloId
                        select ko.Naziv).First();


            r.RpKlGodina = (from ko in Kola
                            where ko.ID == b.KoloId
                            select ko.Godina).First();


            r.RpKlDatum = (from ko in Kola
                           where ko.ID == b.KoloId
                           select ko.Datum).First();


            r.RpBjBoja = (from ko in Kola
                          where ko.ID == b.KoloId
                          select ko.Boja).First();


            r.RpDobIznos = (from d in Dobici
                            where d.Kolo == b.Kolo
                            select d.Iznos).First();


            _report.Add(r);


        }

    }

3 个答案:

答案 0 :(得分:1)

我建议你使用扩展方法,我得到了它的工作,但它可能会得到改善(比如,检查我没有负面的输入值...),但我会离开为你:

public static class MyExtentionMethods
{
    public static ObservableCollection<T> TakeSomeIgnoreSome<T>(this ObservableCollection<T> collection, int numberGet, int numberIgnore)
    {
        var col = new ObservableCollection<T>();
        var enumerator = collection.GetEnumerator();
        int counter = 0;
        bool getting = true;
        while(enumerator.MoveNext())
        {
            if (getting)
                col.Add(enumerator.Current);

            counter++;

            if (getting && counter == numberGet || !getting && counter == numberIgnore)
            {
                getting ^= true;
                counter = 0;
            }

        }
        return col;
    }
}

我可以这样做:

var coll = new ObservableCollection<string>() 
        {
            "test1","test2","test3","test4","test5","test6","test7","test8","test9","test10","test11","tes12t","test13","test14","test15"
        };
        var res = coll.TakeSomeIgnoreSome(3,4); // returns 1,2,3,8,9,10,15

答案 1 :(得分:0)

您的收藏品似乎需要paginate选项。 您可以使用Util Method来为您完成。

以下是List,您可以为Observable Collection实现自己的。

    public static List<T> PaginateWithOffset<T>(List<T> list, int offset, int pageSize)
    {
        List<T> tempList = new List<T>();
        if (offset < 0 || pageSize < 0 || offset >= list.Count || list.Count == 0)
        {
            return list;
        }
        else
        {
            int endPage = offset + pageSize;
            int startPage = offset;
            if ((startPage < list.Count && endPage > list.Count) || (pageSize == 0))
            {
                endPage = list.Count;
            }

            for (int i = startPage; i < endPage; i++)
            {
                tempList.Add(list[i]);
            }

            return tempList;
        }
    }

答案 2 :(得分:0)

就个人而言,我只是将foreach迭代器作为一个整数跟踪,并在循环中做一些检查以确定您是否对当前项感兴趣,方法是将迭代器值与&#34; skip&#进行比较34;价值和&#34;保持&#34;值。

class Program
{
    static void Main(string[] args)
    {
        var populatedList = new List<String>
        {
            "One", "Two", "Three", "Four", "Five",
            "Six", "Seven", "Eight", "Nine", "Ten"
        };

        var fillThisList = new List<String>();

        int itr = 1;
        int keep = 3;
        int skip = 7;
        foreach (var item in populatedList)
        {
            if (itr == skip)
            {
                // reset the iterator
                itr = 1;
                continue;
            }
            if (itr <= keep)
            {
                fillThisList.Add(item);
            }
            itr++;
        }
    }
}

fillThisList将填充&#34; One&#34;,&#34; Two&#34;,&#34; Three&#34;,&#34; Eight&#34;,& #34; Nine&#34;,&#34; Ten&#34;。