2017-06-21 80 views
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」 的象徵性PayCurrency和RecCurrency存在於「refData」中。

輸出:

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=""} 
     }; 

回答

1

爲C#6及以上,你可以做類似下面

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(); 

對於舊版本的

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(); 

Here is working example

更新使用@SAJ回答

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 Trade { Id=r.Id,PayIndex=r.PayIndex,RecIndex=r.RecIndex, RecCurrency = a != null ? a.Symbol : "", PayCurrency = s != null ? s.Symbol : ""}).ToList(); 
+0

感謝jitender。它工作但有點慢。 – SMA

+0

很高興幫助我更新了使用連接的答案,看看它是否工作得更快 – jitender

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; 
     });