Reputation: 107
Hi everyone I have an observable collection and what I want is to create a new observable collection that will take the first 6 objects of the original one and skip the next 12 but on a loop so take 6 skip 12 take 6 skip 12 as long as there are objects in there.
I have read about the take and skip methods and have used them but to little effect. If I say take 6 it will take the first 6 and then stop without looping if I do the take 6, skip 12 it will never even go into the loop just skip it and so on and so forth. Hope u guys can help here is some code.
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);
}
}
Upvotes: 0
Views: 2444
Reputation: 181
Personally, I would just keep track of the foreach iterator as an integer and do some checks within the loop to determine whether you are interested in the current item or not, by comparing the iterator value with a "skip" value and a "keep" value.
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
will then be populated with "One", "Two", "Three", "Eight", "Nine", "Ten".
Upvotes: 0
Reputation: 149
I propose you use an extention method, I got this working, but it can probably be improved (like, checking that I don't have negative input values...), but I'll leave that for you:
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;
}
}
I can then do:
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
Upvotes: 1
Reputation: 2638
It seems you need paginate option for your collection. You can have Util Method which will do it for you.
Following is for List, you can implement your own for 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;
}
}
Upvotes: 0