Skip to content

Instantly share code, notes, and snippets.

@cbuctok
Created July 25, 2019 11:00
Show Gist options
  • Save cbuctok/227bc3721d6763a969eb76f4ef31ffa3 to your computer and use it in GitHub Desktop.
Save cbuctok/227bc3721d6763a969eb76f4ef31ffa3 to your computer and use it in GitHub Desktop.
Group By Index
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var s = new List<int> { 1,2,3,34,5,678,8,5,4,5,7,8,7,4,3,3,6,8,4};
var groups = s.Select((value,index)=>new { Value= value,Index=index}).GroupBy(i=>i.Index/2,v=>v.Value);
var x = groups.ToList();
foreach (var t in x){
var xx = t.ToList();
Console.WriteLine(xx.Count != 2 ? "ONE element " + xx.First() : "TWO elements " + xx.First() + " " + xx.Last());
}
Console.WriteLine("Hello World");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment