1

I'm stuck in a block of code, I'm attempting to pass an object to a chart, the actual data is being send as {shop: 'xyz', total: 20}, but the chart queries only {'xyz', 20}, thing is, the data is coming from a query in my database, which returns me those 2 values

List<Object> list = new List<Object>();

while (resSCIPA.Read())
{

    object shopping = resSCIPA["shopping"];
    object total = resSCIPA["total"];

    var item = new
    {
        shopping, 
        total
    };

    list.Add(item);
}
SerieDataListModel seriesAll = new SerieDataListModel()
{
    name = "Preventiva",
    data = list
};

the list is going to receive {'xyz', 20}, how can I achieve this info from my actual loop?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Matheus Batista
  • 305
  • 1
  • 3
  • 12
  • If you don't want the total, don't include the total in your object. In other words `var item = new { shopping };`... – Heretic Monkey Feb 18 '19 at 20:38
  • no, the shopping = xyz, and the total = 20, I need them both without the atribute name, only their values – Matheus Batista Feb 18 '19 at 20:39
  • Oh, you want the value of shopping to be the property name? – Heretic Monkey Feb 18 '19 at 20:40
  • I don't want neither property name, just their values so the chart can read the data correctly – Matheus Batista Feb 18 '19 at 20:41
  • 1
    Then you don't want an object, you want an array. Use array semantics then `var item = new dynamic[] { shopping, total }`. – Heretic Monkey Feb 18 '19 at 20:42
  • What is resSCIPA? Does it not have value property. Like resSCIPA["shopping"].Value ?? – Hasta Tamang Feb 18 '19 at 20:42
  • 1
    thanks @HereticMonkey, the dynamic[] did the work! – Matheus Batista Feb 18 '19 at 20:45
  • This feels like a duplicate of: https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – Dessus Feb 18 '19 at 20:48
  • @Dessus Not really, since it has nothing to do with JSON... Maybe [C# array of different objects](https://stackoverflow.com/q/17360545/215552)...? – Heretic Monkey Feb 18 '19 at 20:49
  • Perhaps this is the way to solve it https://stackoverflow.com/questions/29632593/parsing-json-key-value-pairs-with-json-net/29632704 You can then filter down to just the values as wanted, and it would work with any flat JSON structure. If its nested then you would need some sort of recursion like technique. – Dessus Feb 18 '19 at 20:56

1 Answers1

2

Since you want an array of values, use an array type to capture them:

var item = new dynamic[] { shopping, total };

Or an object if dynamic causes problems:

var item = new object[] { shopping, total };
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122