tags:

views:

181

answers:

2

When deriving combination,what is the possible way to generate auto generate numbers.

public enum Color
{
    Red,Green,Blue
}

public enum Vehicle
{
    Car,Bike
}

(i.e) {Red,Car},{Red,Bike},{Green,Car},{Green,Bike}......

(Jon Skeet helped me to solve it).

var query = from Color c in Enum.GetValues(typeof(Color))
            from Vehicle v in Enum.GetValues(typeof(Vehicle))
            select new { Color = c, Vehicle = v };

Now I want the combination like this

{1,Red,Car},{2,Red,Bike},{3,Green,Car},{4,Green,Bike},{5,Blue,Car},{6,Blue,Bike}

What is the way to generate auto numbers?

+2  A: 

Try:

int optionNumber = 0;
var query = from Color c in Enum.GetValues(typeof(Color))
            from Vehicle v in Enum.GetValues(typeof(Vehicle))
            select new { Number = optionNumber++, Color = c, Vehicle = v };
eglasius
+1  A: 

Another option is to use the overloaded Select method that includes an item's index. Building on your original query, you could use this:

var indexedQuery = query.Select((item, i) => new { Index = i + 1, Item = item });
foreach (var o in indexedQuery)
{
   Console.WriteLine("{0},{1},{2}", o.Index, o.Item.Color, o.Item.Vehicle);
}
Ahmad Mageed