四个字符串数组c#的特定顺序问题 [英] Specific order issue for four strings array c#

查看:94
本文介绍了四个字符串数组c#的特定顺序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对四个字符串数组的特定顺序有疑问。我将描述Inupt值,输出值和我想要的输出值。



字符串:



I have a problem regarding an specific order of four string arrays. I'll describe what the Inupt values, my Output values and my desired Output values.

Strings:

string[] param, vol, tp, curves;





输入表:



Input Table:

curves | vol | tp | param

curve1 | vo1 | t1 | val1
curve2 | vo1 | t1 | val2
curve3 | vo1 | t1 | val3
curve1 | vo1 | t2 | val4
curve2 | vo1 | t2 | val5
curve3 | vo1 | t2 | val6
curve1 | vo1 | t3 | val7
curve2 | vo1 | t3 | val8
curve3 | vo1 | t3 | val9





我的输出表:



My Output Table:

vol |  t1  |  t2  | t3

vo1 | val1 |      |   
vo1 | val2 |      |  
vo1 | val3 |      |  
vo1 |      | val4 |  
vo1 |      | val5 |  
vo1 |      | val6 |  
vo1 |      |      |  val7
vo1 |      |      |  val8
vo1 |      |      |  val9





使用此代码:



Using this code:

for (int l = 0; l < vol.Length; l++)
{
    mWSheet1.Cells[10 + l, 1] = vol[l];

    if (tp[l] == "t1")
        mWSheet1.Cells[10 + l, 2] = val[l];
    if (tp[l] == "t2")
        mWSheet1.Cells[10 + l, 3] = val[l];
    if (tp[l] == "t3")
        mWSheet1.Cells[10 + l, 4] = val[l];
}





我想要的输出表:



My desired Output Table:

vol |  t1  |  t2  | t3

vo1 | val1 | val4 | val7   
vo1 | val2 | val5 | val8   
vo1 | val3 | val6 | val9 





结果表必须在mWSheet1工作表中发布,第一列是vol,第二列是t1,第三列是t2,第四个t4。你能帮帮我吗?我做错了什么?



提前谢谢:)



注意:

tp字符串数组是字符串数组param中某个值的TimePoints数组。来自所需输出表的列t1,t2和t3是tp字符串数组中出现的不同值



The result table must be released in mWSheet1 worksheet with the first column being the vol, the second t1, the third t2, the fourth t4. Can you help me with this? What I'm doing wrong?

Thanks in advance :)

Notes:
"tp" string array is the array of TimePoints for a certain value in the string array "param". The columns t1, t2 and t3 from the desired Output table are the distinct values that occur in the "tp" string array

推荐答案

正如Bill所说,数据透视表中的Excel可能是最好的方法。



但是,如果您仍想在C#中执行此操作,则需要跟踪每个<的第一行 curve 值出现,并更新该行而不是使用循环索引:

As Bill said, a Pivot Table in Excel would probably be the best approach.

However, if you still want to do this in C#, you'll need to keep track of the first row that each curve value appears on, and update that row rather than using the loop index:
var curveRows = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
for (int index = 0; index < vol.Length; index++)
{
    int curveRow;
    if (!curveRows.TryGetValue(curves[index], out curveRow))
    {
        curveRow = 10 + curveRows.Count;
        curveRows.Add(curves[index], curveRow);
    }
    
    mWSheet1.Cells[curveRow, 1] = vol[index];
    
    switch (tp[index])
    {
        case "t1":
        {
            mWSheet1.Cells[curveRow, 2] = param[index];
            break;
        }
        case "t2":
        {
            mWSheet1.Cells[curveRow, 3] = param[index];
            break;
        }
        case "t3":
        {
            mWSheet1.Cells[curveRow, 4] = param[index];
            break;
        }
    }
}


这篇关于四个字符串数组c#的特定顺序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆