Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple data types to list

I have my list as below,

var serie_line = new { name = series_name , data =new List<float?>() };

In the above code data in another list which contains float value, I want data to contains two different datatype value that is string and float value, when I am trying to add two different datatype values as follow,

var serie_line = new { name = series_name , data =new List<string, float?>() };

It gives me an error as Using the generic type'System.Collections.Generic.List<T>' requires 1 argument.

I cannot try for data=new List<Tupple<string,float>>();..since I am using .NET 3.5...any idea..hw cn I deal with this problem..thank you,

----------Updated question---------

Output that I requires is as follows,

 {
 "legend":{"enabled":"true"},
 "title":{"text":"Financial"},
 "chart":{"type":"pie"},
 "series":
  [
    {"name":"Actual-","data":[["Market Share",20.00],["Sales Growth",30.00],["Operating Profit",40.00],["Actual-Gross Margin %",10.00]]}
  ]
  },

this data list should contains one string value and one float value...I want to draw pie chart in highcharts but output I am getting is as follows,

{
"legend":{"enabled":"true"},
"title":{"text":"Financial"},
"chart":{"type":"column"},
"series":[{"name":"Actual","data":[{"Str":"Market Share","Flo":20.00}]},
          {"name":"Actual","data":[{"Str":"Sales Growth","Flo":30.00}]},
          {"name":"Actual","data":[{"Str":"Operating Profit","Flo":40.00}]},
          {"name":"Actual","data":[{"Str":"Gross Margin %","Flo":10.00}]}
         ]
}

Any Idea...???

----------Use of Dictionary----------

var data = new Dictionary<string, float?>();
var serie_line = new { name = series_name, data };
serie_line.data.Add(child_object_name, period_final_value);

but this doesnot give required output... it only gives values inside data as for eg, "data":["market share":20.00].. since I am serializing serie_line into JSON...but I don't want this way..what I want is "data":["market share",20.00]

I hope u get this...

like image 581
Reshma Avatar asked Dec 12 '25 09:12

Reshma


2 Answers

just use

 new Dictionary<string, float?>() //if your string value cannot be duplicated  

//or

  new  List<KeyValuePair<string,float?> > 
like image 187
BRAHIM Kamel Avatar answered Dec 13 '25 21:12

BRAHIM Kamel


create a type to be use with your list:

public class MyDataType
{
  public string Str {get; set;}
  public float? Flo {get;set;}
}

you use it like this:

var serie_line = new { name = series_name , data =new List<MyDataType>() };
serie_line.data.Add(new MyDataType{Flo = 45.4});

or like:

var serie_line = new { name = series_name , data =new List<MyDataType>() };
serie_line.data.Add(new MyDataType{Flo = 45.4, Str = "my string"});
like image 37
Jens Kloster Avatar answered Dec 13 '25 21:12

Jens Kloster