如果/ else查询,请更改LINQ脚本

时间:2014-01-31 19:44:28

标签: c# linq

我正在尝试修改一个如下所示的列表:

{"BAY-T", "HBC-T", "AAPL-O", "ABC'D-T", "MAN'D-O", "GBL-LN"}

需要更改为:

{"BAY.TO", "HBC.TO", "AAPL", "ABC.D.TO", "MAN.D", "GBL.L"}

有没有办法在LINQ中执行此操作?这是我到目前为止所得到的:

var cleanedupCompList = Companies
.Select (c => new {
    Tick = c.Fc_ticker.Contains("'") ? c.Fc_ticker.Replace("'", ".") : 
        c.Fc_ticker.Contains("-T") ? c.Fc_ticker.Replace("-T", ".TO") :
        c.Fc_ticker.Contains("-O") ? c.Fc_ticker.Replace("-O", "") :
        c.Fc_ticker.Contains("-LN") ? c.Fc_ticker.Replace("-LN", ".L") :
        c.Fc_ticker});

然而,我没有得到我想要的结果。我需要改变什么?

2 个答案:

答案 0 :(得分:2)

您无需执行Contains()检查。只需要替换:

c.Fc_ticker.Replace("'", ".").Replace("-T", ".TO")
               .Replace("-O", "").Replace("-LN", ".L")

答案 1 :(得分:1)

试试这个:

var Companies = new [] {"BAY-T", "HBC-T", "AAPL-O", "ABC'D-T", "MAN'D-O", "GBL-LN"};
var cleanedupCompList = Companies
      .Select (c => c.Replace("'", ".").Replace("-T", ".TO").Replace("-O", "").Replace("-LN", ".L"));