问题
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
回答1:
Well, you're explicitly removing type information by calling AsEnumerable<object>
. Just get rid of that, and use var
for the variable:
var 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 }).ToList();
Then you can do:
foreach (var item in test)
{
Console.WriteLine(item.a); // Strongly typed at compile-time
}
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:
- Create a named type with all the relevant data, so you can keep it in that form in a strongly typed way.
- Use type inference for the method that you're passing the data to (that may or may not be feasible; it's not clear what you're doing).
- Use dynamic typing in C# 4 to get around normal static typing in C#.
- Access the properties using reflection.
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.
回答2:
If you are stripping out all of the compile time type information and can't use Dynamic objects then you will end up having to use reflection.
var i=test.FirstOrDefault();
if (i==null) {
// NO ITEMS IN LIST!!!
// So do something else!
}
var type=i.GetType();
var aGetter=type.GetProperty("a");
var bGetter=type.GetProperty("b");
foreach (var item in test) {
bool myA = (bool)aGetter.GetValue(item,null);
int myB=(int)bGetter.GetValue(item.null);
}
回答3:
you can use reflection:
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 g in para4
let f = c - d
where c - d <= 3
select new { a, b, c, d, g, f }).AsEnumerable<object>().ToList();
foreach (object item in test)
{
Response.Write(item.GetType().GetProperty("a").GetValue(item, null).ToString());
}
you can also give names to your fields like this:
select new { aName = a, bName = b, cName = c, dName = d, gName = g, fName = f }).AsEnumerable<object>().ToList();
and then use:
foreach (object item in test)
{
Response.Write(item.GetType().GetProperty("aName").GetValue(item, null).ToString());
}
来源:https://stackoverflow.com/questions/6830367/linq-to-listobject-and-get-values-per-row