Skip to content

Instantly share code, notes, and snippets.

@tufanbarisyildirim
Last active December 16, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tufanbarisyildirim/5422553 to your computer and use it in GitHub Desktop.
Save tufanbarisyildirim/5422553 to your computer and use it in GitHub Desktop.
select list items by using print page like string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// TODO: check out of page ranges.
public static class GenericExtensions
{
public static List<T> PrintPagesFilter<T>(this List<T> list, string pattern)
{
List<T> NewList = new List<T>();
foreach (string segment in pattern.Split(','))
{
if (segment.Contains('-'))
{
string[] limits = segment.Split('-');
int firstpage = Convert.ToInt32(limits[0].Trim());
int lastpage = Convert.ToInt32(limits[1].Trim());
int startindex = firstpage - 1;
int count = lastpage - startindex;
NewList.AddRange(list.GetRange(startindex, count));
}
else
{
NewList.Add(list[Convert.ToInt32(segment.Trim()) - 1]);
}
}
return NewList;
}
public static List<List<T>> Split<T>(this List<T> source, int junkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / junkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
public static List<List<T>> SplitColumn<T>(this List<T> source, int ColumnSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index % (source.Count() / ColumnSize))
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> Pages = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }.ToList();
List<int> PrintPages = Pages.PrintPagesFilter("1-5,10,11,15-20");
Console.WriteLine(String.Join(",", PrintPages.Select(i => i.ToString()).ToArray()));
// Output : 1,2,3,4,5,10,11,15,16,17,18,19,20
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment