1

I have a list of c# objects and each object has 100 properties:

public string Group1;
public string Group2;
public string Group3;

.....................
...
..
.
public string Group99;
public string Group100;

I want to be able to pass in two numbers in the range of 1 to 100 and only get the properties that fall in between that range.

for example if i pass in the number 31 to 50 i would want the properties:

public string Group31;
public string Group32;

....................
...
..
.
public string Group50;

how would I be able to achieve this?

Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121
  • Just to check this is just sample code and your actual code is more complex? At the moment the above looks like you are better off with a dictionary holding strings rather than this. Also Whether the property names are uniform like this will likely make a difference to the answer... – Chris Jul 06 '13 at 10:08
  • 1
    Those are fields. Do you *actually* have fields, or do you have properties? And why don't you just have a collection instead? – Jon Skeet Jul 06 '13 at 10:08
  • 1
    You will need a `Reflection` for this task. However array or list usage looks much more appropriate, as said above. – Tommi Jul 06 '13 at 10:09

1 Answers1

1

In your case you have fields, so you can use reflection and LINQ like this:

//pass your class to typeof
var ClssType = typeof (SomeCLass);
ClssType.GetFields().OrderBy(n=>n.Name).Skip(30).Take(19).ToList();

In Skip you passing numbers that you want to skip befaure taking fields.

If you had properties you could use .GetProperties() instead of .GetFields()

For getting values of your properties you need to call .GetValue(obj, null) for every object in your array.

   //let say you have array of objects myObj[] then your code will look like this:
   var fieldsInfos = ClssType.GetFields().OrderBy(n=>n.Name).Skip(30).Take(19).ToList();
   //go thorugh your array
   foreach(var obj in myObj)
   { 
       //go through fields
       foreach(var field in fieldsInfos)
       {
           //get value of field by calling
           Console.WriteLine(field.GetValue(obj, null));
        }     
   }
Oleksii Aza
  • 5,368
  • 28
  • 35
  • do I have to do this on each object or can I apply this to all the objects in the list? – Farhad-Taran Jul 06 '13 at 10:14
  • Are you sure that GetFields will return sorted fields array? Otherwise direct skip/take will make no sense. – Tommi Jul 06 '13 at 10:17
  • Are you sure that OrderBy will sort list not like P1, P10, .., P2, P20, ..? – Viktor Lova Jul 06 '13 at 10:24
  • 1
    There is one more caveeat: fields names in OP sample are not use digits padding and default sorting will be not `natural`, so order will be field1, field10, field11, field2, field20; so additionally you'll need to create "natural" implementation of IComparer, since there is no built-in in .net. http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp – Tommi Jul 06 '13 at 10:25
  • That's why Chris and Jon Skeet recommended to use generic collection for op's task. – Tommi Jul 06 '13 at 10:27
  • Yes, now I see the problems of my algorithm, thank you for your clarifications. – Oleksii Aza Jul 06 '13 at 10:31