如何从参数创建“公共字符串数组”作为数组?

时间:2018-06-13 09:23:10

标签: c# elasticsearch arraylist parameters

我有这段代码:

public static void SendDataToES(String startTimestamp, String startDate, String bid_x, String ask_x)
{
    var tem_ehm = new Pre_Market
    {
        timestamp = startTimestamp,
        date = startDate,
        bid = bid_x,
        ask = ask_x
    };
}

class Pre_Market
{
    public string x_ric { get; set; }
    public string ask { get; set; }
    public string bid { get; set; }
    public string date { get; set; }
    public string timestamp { get; set; }
}

但将来它会将参数作为数组。

public static void SendDataToES(String startTimestamp,String startDate,String bid_x,String ask_x, IList nameABC,String [] getABCs

其中nameABC []的值为A,B,C和getABC []的值为1,2,3所以我想在类Pre_Market中创建为Array

public string[] A { get; set;}
public string[] B { get; set;}
public string[] C { get; set;}

不确定以下工作正常吗?

for ( int i = 0 ; i < nameABC.Count(); i++ )
{
  public string[] nameABC[i] { get; set; }
}

以便可以使用以下内容?

var tem_ehm = new Pre_Market
{
    timestamp = startTimestamp,
    date = startDate,
    bid = bid_x,
    ask = ask_x,
    A = getABC[0],
    B = getABC[1],
    C = getABC[2]
};

更新了!以下工作正常。

var temp_ehm = new Pre_Market
{
    timestamp = startTimestamp,
    date = startDate,
    fids = new Dictionary<string, string>(),
};

for (int i = 0; i < nameFIDs.Count() - 1; i++)
{
    temp_ehm.fids.Add(nameFIDs[i], get_FIDs[i]);
}

“temp_ehm中的fids可以添加”这对我来说是新闻!

2 个答案:

答案 0 :(得分:0)

如果您可以使用匿名类而不是Pre_Market,那么您可以使用JSON构建对象结构(构建字符串很简单),然后使用JSON反序列化器(install NewtonSoft.JSON via NuGet )将其转换为对象

这里您可能想要生成JSON字符串

string jsonStr = "{ timestamp = \"" + startTimestamp + "\"," +
    "date = \"" + startDate + "\"," +
    "bid = \"" + bid_x + "\"," +
    "ask = \"" + ask_x + "\"";

for ( int i = 0 ; i < nameABC.Count(); i++ )
{
  jsonStr  += "," + nameABC[i] + " = \"" + getABCs[i] + "\""
}

jsonStr += "}";

答案 1 :(得分:0)

一个简单的解决方案是使用字典。

public static void SendDataToES(String startTimestamp, String startDate, String bid_x, String ask_x, IList<string> nameABC, string[] getABCs)
{
    var tem_ehm = new Pre_Market
    {
        timestamp = startTimestamp,
        date = startDate,
        bid = bid_x,
        ask = ask_x,
        ABCs = new Dictionary<string, string>()
    };
    int i = 0;
    foreach (string name in nameABCs)
        tem_ehm.Add(name, getABCs[i++];

    // access the ABCs like so:
    string A = tem_ehm.ABCs["A"];
    string B = tem_ehm.ABCs["B"];
    string C = tem_ehm.ABCs["C"];
}

class Pre_Market
{
    public string x_ric { get; set; }
    public string ask { get; set; }
    public string bid { get; set; }
    public string date { get; set; }
    public string timestamp { get; set; }
    public Dictionary<string, string> ABCs { get; set; }
}