2

I have a statement like this =

var result =
    from c in displayedUsers
    select
    new string[]
    {
        c.GetType().GetProperty(columnList[0]).GetValue(c, null).ToString(),
        c.GetType().GetProperty(columnList[1]).GetValue(c, null).ToString(),
        c.GetType().GetProperty(columnList[2]).GetValue(c, null).ToString(),
        c.GetType().GetProperty(columnList[3]).GetValue(c, null).ToString()
    };

What I want to know is if it's possible to not have to have a static list length (in this case you can see there are only 4 items)

How would I do this if I had N number of columns in the 'columnList' array?

Something along the lines of =

var result =
    from c in displayedUsers
    select
    new string[]
    {
        foreach item in columnList GetValue
        ...
    };

Thanks!

1
  • What Type is columnList? Commented Dec 9, 2013 at 22:43

2 Answers 2

2

Something like this should work:

var columnList = new []{"cola", "colb"};
var result = from c in displayUsers
             select
             (
                 (
                     from col in Columnlist
                     select c.GetType().GetProperty(col).GetValue(c, null).ToString()
                 ).ToArray();
             )
Sign up to request clarification or add additional context in comments.

Comments

0

If columnList can be cast or converted to a List then there's the ForEach method that could be used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.