Express Yourself with C#'s Query Syntax: Listing 1: C#: Take Advantage of Nested From Clauses

From clauses can be nested to iterate all the members of the nested sequence. You can either flatten the sequence, as I've done here, or create a sequence of values from the inner and outer sequences.

public class LabeledSequence
{
   public string Label
   {
      get;
      set;
   }

   private List<int> sequence = new List<int>();
   public IList<int> Sequence
   {
      get { return sequence; }
   }
}

List<LabeledSequence> stuff = new 
   List<LabeledSequence>
{
   new LabeledSequence{Label = "Single Digit Numbers", 
      Sequence = {0,1,2,3,4,5,6,7,8,9}},
   new LabeledSequence{Label = 
      "Small two digit numbers", 
      Sequence = {10,11,12,13,14,15,16,17,18,19}},
   new LabeledSequence{Label = "Some large numbers", 
      Sequence = {256, 512,1024,2048,4096}}
};

var numbers = from seq in stuff
   from num in seq.Sequence
   select num;


Reader Comments:

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above