for a test I need to make a list with some parameter settings. This list is not per definition pre-defined as a type and / or what’s in it.
bool[] trueOrFalse = new bool[] { true, false };
int[] para1 = new int[] { 1, 2, 3 };
int[] para2 = new int[] { 5, 6, 7 };
int[] para3 = new int[] { 1, 2, 3 };
int[] para4 = new int[] { 5, 7, 9 };
List<object> test = (from a in trueOrFalse
from b in para1
from c in para2
from d in para3
from e in para4
let f = c - d
where c - d <= 3
select new { a, b, c, d, e, f }).AsEnumerable<object>().ToList();
the result is fine (this is just for a test) and I do get a list back with all the parameter combinations possible, which I can use as a parameter for another method. (I need it as a list, because, as far as I know, var test cannot be parsed as a parameter to another method.)
When I hover above test in debug mode, I can see each row and each row contains a to f as a separate field.
But how can I subtract the values?
let say I want to use a foreach loop over the list.
how can I set int myA = a etc?
regards,
Matthijs
Well, you’re explicitly removing type information by calling
AsEnumerable<object>. Just get rid of that, and usevarfor the variable:Then you can do:
Now, I had previously missed the “which I can use as a parameter for another method” part. (You can still use a list – but it’s a list of an anonymous type.) Using anonymous types, you really pass the result to a method in a strongly-typed way.
Options:
If you can tell us more about what you’re doing in the other method, that would really help. If the method can be made generic, you may well still be able to use anonymous types.