0

I have generic list foo as shown below

var foo = new List<XYZ>();
public class XYZ
{
    public String TimeZone { get; set; }
    public Decimal? B1Volume { get; set; }
    public Decimal? B2Volume { get; set; }
    public Decimal? B3Volume { get; set; }
    public Decimal? B4Volume { get; set; }
    public Decimal? B5Volume { get; set; }
    // .............
    // .............
    public Decimal? B24Volume { get; set; }
    public String Name {get;set;}
}

how do I select the properties B1Volume,........B24Volume ?

I tried with following code mentioned below, but it's not giving expected results

var hp = foo.Skip(1).Take(23).ToList();
1
  • Of course it doesn't help. Skip , Take ... works on objects not their properties. It works when you have a list like this: {xyz1, xyz2 .... xyz24} Commented Nov 1, 2017 at 20:37

3 Answers 3

1

There's a few ways, but I do not think that you want to go down that road. Do you really want a list of xyz? Or asked in a different fashion: Do you have many different lists of lists of volumes? Or do you only want to express a single list of volumes?

Maybe what you want to do is declare an array inside XYZ like this

public class XYZ
{
    public String TimeZone { get; set; }
    public Decimal?[] Volumes {get; set;} = new Decimal?[24];
    public String Name {get; set;}
}

If you want to access volumes by an index (1,2,...,24) you need an array or any other kind of indexed data structure.

Then you could do

var xyz = new XYZ();
xyz.Volumes[0] = 12.0;
xyz.Volumes[1] = 23.0;
.....

and basically access the volumes by xyz.Volumes and adding an index to get the n-th volume

If you now want to further list these XYZ you could do something like this:

var listOfXyz = new List<XYZ>();
listOfXyz.Add(new XYZ());
....
listOfXyz[3].Volumes

this would give you the 24 volumes of the element at the index of 3 in the list.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to do a Select:

var hp = foo.Select(x => new { x.BVolume1, x.BVolume2, ..., x.BVolume24 });

Although I do agree with @Himzo that this is not the best way to solve your problem if you can change the structure.

Comments

0

Maybe it helps:

XYZ xyz = new XYZ();
Type t = xyz.GetType();
List<PropertyInfo> properties = new List<PropertyInfo>(t.GetProperties());
var hp = properties.Skip(1).Take(23).ToList();

Do not forget adding name space:

using System.Reflection;

Update

In comments GBreen12 suggests to add a filter for getting only properties that has name containing volume. Now if you add another properties the code will not fail. So you can change the 3th line to this:

List<PropertyInfo> properties = (new List<PropertyInfo>(t.GetProperties())).Where(x => x.Name.EndsWith("Volume")).ToList();

Now you do not need last line var hp = ... and the properties is your answer.

8 Comments

I would suggest not assuming the return from GetProperties is the same order as the declaration (though that is the case with the current compiler) and filter on the property names.
@NetMage I tested the exact class XYZ and properties are in same order.
@FarzinKanzi I think what NetMage is trying to say is that it's not really safe to assume the order of the properties for various reasons. Even if right now they return in the right order, the object definition could change in any number of ways that would screw that up.
@GBreen12 Thanks. What you mean object definition could change in any number of ways?
I feel like if you're going to do it this way, you should do some sort of Where like .Where(x => x.Name.EndsWith("Volume")). In which case you wouldn't need the Skip or Take
|

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.