Pavel

Pavel

  • 1.6k
  • 170
  • 51.2k

Cannot use the output of LINQ query

Sep 4 2012 5:12 AM

Hello,

The initial task is to find all permutations of the byte array.
byte [] SFP = new byte[8] { 0, 1, 2, 3, 4, 5, 6, 7 };

I've found on some forum the following procedure:
public static IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> list)

{if (list.Count() == 1)

  return new List<IEnumerable<T>> { list };

return list.Select((a, i1) => Permute(

list.Where((b, i2) => i2 != i1)).Select(

b => (

new List<T> { a }).Union(b))

).SelectMany(c => c);

}

I use it in this way: var SFP_vars = Permute(SFP); It works fine - the query find all combinations.
T
he problem is that I can't use foreach to have access to particular memberes of list.
The foreach loop (see below) provoke the runtime error:
InvalidCastException was unhandled. Unable to cast object of type '<UnionIterator>d__88`1[System.Byte]' to type 'System.Byte[]' 

Construction, that give error:
foreach (
byte[] SFP_var in SFP_vars)
{
}

How to circumvent the problem.

Thanks in advance.

Pavel.

 

 

 

 

 


 


Answers (6)