I'm iterating through objects that contains an array of doubles. Names changed. Imagine this is an object of Lab samples, and I'm trying to get a flat representation of the Sample object.
From:
Sample
{
public string Name
public string Type
...
public double[] RawReadings
}
To, for example:
Name,
RawRead1,
RawRead2,
RawRead3,
...
RawReadn
How do I finangle sample.RawReadings to give me all n items in the array? I don't care if it's named or not.
Edit for clarity: I want the each item in the resulting list to have the Name and as many doubles as properties in the class as there are RawReads. I do not want each item in the resulting list to have a Name and an array - that'll just be the same as what I had.
Sample LinQ:
from sample in Samples
select new
{
Name = sample.Name,
sample.RawReadings
}
Is this even possible?
Edit: I'm feeding this to a 3rd party API call that's expecting something in a "row and column format". Aspose.Cells ImportCustomObject to be exact. It might be possible to work around it with multiple calls and manually aligning them but that can be tricky and bug prone.
I want the each item in the resulting list to have the Name and as many doubles as there are RawReads.
You can't do that using anonymous types. Anonymous types have property names and types specified at compile-time whereas you're wanting to specify them at execution-time. It's not clear why you're trying to do this, but the closest you could come would be to use dynamic typing, for which you could use an expando:
public dynamic SampleToDynamic(Sample sample)
{
IDictionary<string, object> expando = new ExpandoObject();
expando["Name"] = sample.Name;
for (int i = 0; i < sample.RawReadings.Length; i++)
{
expando["RawRead" + (i + 1)] = sample.RawReadings[i];
}
return expando;
}
See more on this question at Stackoverflow