How to add multiple data types to list

2019-05-27 03:40发布

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...

5条回答
欢心
2楼-- · 2019-05-27 04:08

If you are trying to add items to a list so that both are available you need to use List<object>(), as its the only shared type between both. That or use ArrayList.

As you pull the objects out you will need to test if they are objects of type string or float? in order to cast them back. You may be able to wrap them.

查看更多
唯我独甜
3楼-- · 2019-05-27 04:09

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"});
查看更多
爷、活的狠高调
4楼-- · 2019-05-27 04:11

just use

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

//or

  new  List<KeyValuePair<string,float?> > 
查看更多
The star\"
5楼-- · 2019-05-27 04:12

How about something more structured:

public struct MyData
{
    public float? FloatData;
    public string StringData;
}

var serie_line = new
                 {
                     name = series_name,
                     data = new MyData()
                            {
                                FloatData = theFloatData,
                                StringData = theStringData,
                            }
                 };
查看更多
看我几分像从前
6楼-- · 2019-05-27 04:17

Use ArrayList, the non-generic version of List.

查看更多
登录 后发表回答