Wednesday 15 August 2012

linq - C# -Getting Possible pairs from two enums -


With two enums, what is the method of implementing LINQ to get the pair

like < / P>

{red, car}, {red, bike}, {green, car}, {green, bike}, ...

  public enum color (red, green , Blue} public Enum vehicle {car, bike}  

Do I like something like

  var in query = c Enum.GetValues ​​(typeof (color) )) Can be used in Enum.GetValues ​​(typeof (vehicle)) from C asQueryable (). Select AsQueryable (New) .. to fill in here? ..}  

do not use

c Do not use the asQueryable twice as a bar, unless you really need it, actually use an anonymous type in a simple way, and problems caused by Enum.GetValues Specify the type of category variable to avoid, just return Array :

  var returning query = Enum.GetValues ​​(typef (color)) in Enum C Typef (Vehicle) in Vehicle V with Color in GETValues )) Select new {color = c, vehicle = V};  

(This is equivalent to calling . and .Cast related Enum.GetValues ​​ call.)

Then you can write it like this:

  foreach (var pair in query) {Console.WriteLine ( "{{{{}, {1}}}", pair color, couple.Winil); }  

No comments:

Post a Comment