public
Last active

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
//http://arbel.net/2008/08/18/writing-methods-and-classes-in-linqpad/
//Choose "C# Statement(s)" in the Type drop-down.
//Write the code that LINQPad should execute.
//Put a closing curly-bracket ("}") at the end.
//Write as many classes and methods as you like.
//On the last class/method, omit closing curly-bracket.
List<KeyValuePair<int, int>> housePersonPairs = new List<KeyValuePair<int, int>>();
housePersonPairs.Add(new KeyValuePair<int, int>(1, 11));
housePersonPairs.Add(new KeyValuePair<int, int>(1, 12));
housePersonPairs.Add(new KeyValuePair<int, int>(1, 13));
housePersonPairs.Add(new KeyValuePair<int, int>(2, 232));
housePersonPairs.Add(new KeyValuePair<int, int>(2, 5533));
housePersonPairs.Add(new KeyValuePair<int, int>(2, 40));
List<Person> persons = new List<Person>();
persons.Add(new Person() { ID = 11, Name = "John" });
persons.Add(new Person() { ID = 12, Name = "Jane" });
persons.Add(new Person() { ID = 13, Name = "Zoe" });
persons.Add(new Person() { ID = 232, Name = "Name1" });
persons.Add(new Person() { ID = 5533, Name = "Name2" });
persons.Add(new Person() { ID = 40, Name = "Name3" });
 
var houseAndNames = housePersonPairs.Join(
persons,
hpp => hpp.Value,
p => p.ID,
(hpp, p) => new { HouseID = hpp.Key, Name = p.Name });
 
var groupedNames = from hn in houseAndNames
group hn by hn.HouseID into groupOfNames
select groupOfNames.Select(x => x.Name).ToList();
 
List<House> houses = groupedNames.Select(names => new House() { people_name = names }).ToList();
System.Console.WriteLine(houses);
 
}
 
public class Person {
public int ID { get; set; }
public String Name { get; set; }
}
 
class House
{
public List<string> people_name { get; set; }
//} //This is deliberately left off so it will work as a Linqpad C# statement.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.