0

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

我已经阅读了 take 和 skip 方法并使用了它们,但收效甚微。如果我说 take 6 它将占用前 6 个然后停止而不循环,如果我执行 take 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);


        }

    }
4

3 回答 3

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
于 2014-08-05T13:34:30.123 回答
0

您的收藏似乎需要分页选项。你可以使用 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;
        }
    }
于 2014-08-05T13:33:24.130 回答
0

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

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然后将填充“一”、“二”、“三”、“八”、“九”、“十”。

于 2014-08-05T14:13:23.530 回答