S
sloan
I'm trying to get a "flat list" of values.
Below is the sample.
Desired Results:
Bobbie Brady has these toys : TeddyBear,Wagon,TonkaTruck
Cindy Brady has these toys : Dollie,TeaSet,Insta-Bake
Some kind of delimiter between the toys would be nice (comma above).
All code below, I'm just missing the magically LINQ statement.
THANKS!
public class Kid
{
public Kid(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
this.FavoriteToys = new ToyCollection();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public ToyCollection FavoriteToys { get; set; }
}
public class Toy
{
public Toy(string name) { this.ToyName = name; }
public string ToyName { get; set; }
}
public class KidCollection : List<Kid>{}
public class ToyCollection : List<Toy>{}
static void Main(string[] args)
{
KidCollection kc = new KidCollection();
kc.Add(new Kid("Bobbie", "Brady"));
kc.Add(new Kid("Cindy", "Brady"));
kc.Add(new Kid("Danny", "Partridge"));
kc[0].FavoriteToys.Add(new Toy("TeddyBear"));
kc[0].FavoriteToys.Add(new Toy("Wagon"));
kc[0].FavoriteToys.Add(new Toy("TonkaTruck"));
kc[1].FavoriteToys.Add(new Toy("Dollie"));
kc[1].FavoriteToys.Add(new Toy("TeaSet"));
kc[1].FavoriteToys.Add(new Toy("Insta-Bake"));
kc[2].FavoriteToys.Add(new Toy("Drums"));
kc[2].FavoriteToys.Add(new Toy("DrumLessons"));
kc[2].FavoriteToys.Add(new Toy("MoreDrumLessons"));
var v = from k in kc
where k.LastName.Contains("Brady")
orderby k.FirstName descending
select k.FirstName + " " + k.LastName + " has these toys : " +
k.FavoriteToys.SelectMany(l => l.ToyName).ToList();
foreach (string s in v)
Console.WriteLine(s);
}
Below is the sample.
Desired Results:
Bobbie Brady has these toys : TeddyBear,Wagon,TonkaTruck
Cindy Brady has these toys : Dollie,TeaSet,Insta-Bake
Some kind of delimiter between the toys would be nice (comma above).
All code below, I'm just missing the magically LINQ statement.
THANKS!
public class Kid
{
public Kid(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
this.FavoriteToys = new ToyCollection();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public ToyCollection FavoriteToys { get; set; }
}
public class Toy
{
public Toy(string name) { this.ToyName = name; }
public string ToyName { get; set; }
}
public class KidCollection : List<Kid>{}
public class ToyCollection : List<Toy>{}
static void Main(string[] args)
{
KidCollection kc = new KidCollection();
kc.Add(new Kid("Bobbie", "Brady"));
kc.Add(new Kid("Cindy", "Brady"));
kc.Add(new Kid("Danny", "Partridge"));
kc[0].FavoriteToys.Add(new Toy("TeddyBear"));
kc[0].FavoriteToys.Add(new Toy("Wagon"));
kc[0].FavoriteToys.Add(new Toy("TonkaTruck"));
kc[1].FavoriteToys.Add(new Toy("Dollie"));
kc[1].FavoriteToys.Add(new Toy("TeaSet"));
kc[1].FavoriteToys.Add(new Toy("Insta-Bake"));
kc[2].FavoriteToys.Add(new Toy("Drums"));
kc[2].FavoriteToys.Add(new Toy("DrumLessons"));
kc[2].FavoriteToys.Add(new Toy("MoreDrumLessons"));
var v = from k in kc
where k.LastName.Contains("Brady")
orderby k.FirstName descending
select k.FirstName + " " + k.LastName + " has these toys : " +
k.FavoriteToys.SelectMany(l => l.ToyName).ToList();
foreach (string s in v)
Console.WriteLine(s);
}