linq to list<object> and get values per row

12,673

Solution 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.

Solution 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);
}
Share:
12,673
user369122
Author by

user369122

Updated on June 04, 2022

Comments

  • user369122
    user369122 almost 2 years

    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

    • BrokenGlass
      BrokenGlass almost 13 years
      If you need to pass this list to another method it almost certainly sounds like you should add a dedicated class - casting to List<object> is just a bad workaround
    • user369122
      user369122 almost 13 years
      You might be right. Will keep that in mind for similar problems in the future.
  • Bob Vale
    Bob Vale almost 13 years
    He says that he is passing the collection as a parameter to the method so he can't use var.
  • user369122
    user369122 almost 13 years
    Hi Bob, thanks for your responds. I'll be using dynamic types for this one.
  • user369122
    user369122 almost 13 years
    hi kleinohad, thank you as well for your responds. Dynamic types will help me out here.