从其他集合更新集合以匹配项目

时间:2017-06-21 03:32:39

标签: linq c#-4.0

我有两个集合,如下所述。如果“refData”中存在这些值,我从其他集合“refData”更新了“trade”的两个属性。

型号:

class Trade
{
    public int Id { get; set; }
    public string PayIndex { get; set; }
    public string RecIndex { get; set; }
    public string PayCurrency { get; set; }
    public string RecCurrency { get; set; }
}
class RefData
{
    public string IndexLabel { get; set; }
    public string Symbol { get; set; }
}

示例日期:

var refData = new List<RefData>
        {
            new RefData { IndexLabel = "A1", Symbol="ABC1"},
            new RefData { IndexLabel = "A2", Symbol="ABC2"},
            new RefData { IndexLabel = "B1", Symbol="BCD1"},
            new RefData { IndexLabel = "B2", Symbol="BCD2"},
        };
        var trades = new List<Trade>
        {
            new Trade { Id = 1, PayIndex = "A1", RecIndex = "B1"},
            new Trade { Id = 2, PayIndex = "A2", RecIndex = ""},
            new Trade { Id = 3, PayIndex = "", RecIndex = "B2"},
            new Trade { Id = 4, PayIndex = "A3", RecIndex = "B3"}
        };

如果交易的PayIndex和RecCurrency存在于“refData”中,我想用符号属性“refData”更新PayCurrency和RecCurrency的“交易”。

输出:

var Output = new List<Trade>
        {
            new Trade { Id = 1, PayIndex = "A1", RecIndex = "B1", PayCurrency = "ABC1", RecCurrency="BCD1"},
            new Trade { Id = 2, PayIndex = "A2", RecIndex = "", PayCurrency = "ABC2", RecCurrency=""},
            new Trade { Id = 3, PayIndex = "", RecIndex = "B2", PayCurrency = "", RecCurrency="BCD2"},
            new Trade { Id = 4, PayIndex = "A3", RecIndex = "B3", PayCurrency = "", RecCurrency=""}
        };

2 个答案:

答案 0 :(得分:1)

对于c#6及以上,你可以这样做

UITableView

旧版本

  var result = trades.Select(t => new Trade() {
       Id= t.Id,
       PayIndex = t.PayIndex,
       RecIndex = t.RecIndex,
       PayCurrency = refData.SingleOrDefault(r => r.IndexLabel.ToLower().Equals(t.PayIndex.ToLower()))?.Symbol ?? "",
       RecCurrency = refData.SingleOrDefault(r => r.IndexLabel.ToLower().Equals(t.RecIndex.ToLower()))?.Symbol ?? ""

    }).ToList();

Here is working example

使用@SAJ答案更新

  var result = trades.Select(t => new Trade() {
       Id= t.Id,
       PayIndex = t.PayIndex,
       RecIndex = t.RecIndex,
       PayCurrency = refData.SingleOrDefault(r => r.IndexLabel.ToLower().Equals(t.PayIndex.ToLower())) != null ?  refData.SingleOrDefault(r => r.IndexLabel.ToLower().Equals(t.PayIndex.ToLower())).Symbol : "",
       RecCurrency = refData.SingleOrDefault(r => r.IndexLabel.ToLower().Equals(t.RecIndex.ToLower())) != null ? refData.SingleOrDefault(r => r.IndexLabel.ToLower().Equals(t.RecIndex.ToLower())).Symbol : ""

    }).ToList();

答案 1 :(得分:0)

你可以试试这个

    var output = (from r in trades
                      join p in refData on r.PayIndex equals p.IndexLabel
                      into g1
                      from s in g1.DefaultIfEmpty()
                      join t in refData on r.RecIndex equals t.IndexLabel into g2
                      from a in g2.DefaultIfEmpty()
                      select new { r, RecSymbol = a?.Symbol, PaySymbol = s?.Symbol }).ToList();

        output.ForEach(o =>
        {
            o.r.PayCurrency = o.PaySymbol;
            o.r.RecCurrency = o.RecSymbol;
        });