簡體   English   中英

Lambda 表達式中的條件

[英]Condition inside a Lambda Expression

我正在學習在我的 lambda 表達式中設置條件。 為此,我查看了其他提出相同問題的其他人的各種示例。

我能夠得到一個工作結果集。 但我目睹的是,在TradeType條件下,lambda 被視為字符而不是字符串,結果返回為char

我在LinqPad工作如下:

void Main()
{
    List<Trade> trades = new List<Trade>();
    trades.Add(new Trade()
    {
        AccountId = "JKB1",
        SecurityId = "JKH.N0000",
        TradeType = "NORMAL",
        Qty = 100,
        Price = 165.50M,
        NetAmount = 16550
    });

    trades.Add(new Trade()
    {
        AccountId = "JKB1",
        SecurityId = "JKH.N0000",
        TradeType = "NORMAL",
        Qty = 1000,
        Price = 166.50M,
        NetAmount = 166500
    });

    trades.Add(new Trade()
    {
        AccountId = "JKB1",
        SecurityId = "HASU.N0000",
        TradeType = "NORMAL",
        Qty = 1000,
        Price = 132.50M,
        NetAmount = 132500
    });

    trades.Add(new Trade()
    {
        AccountId = "JKB2",
        SecurityId = "COMB.N0000",
        TradeType = "NORMAL",
        Qty = 100,
        Price = 137.50M,
        NetAmount = 13750
    });

    trades.Add(new Trade()
    {
        AccountId = "JKB3",
        SecurityId = "JKH.N0000",
        TradeType = "NORMAL",
        Qty = 100,
        Price = 165.50M,
        NetAmount = 16550
    });

    trades.Dump();

    var r = (from t in trades
            group t by new { t.AccountId }
            into g
            select new AccountManagerAccountModel()
            {
                AccountId = g.Key.AccountId,
                AccountName = g.Key.AccountId,
                Securities = g.GroupBy(c1=> new {c1.AccountId, c1.SecurityId, c1.TradeType}).Select(c2=> new AccountStockModel()
                {
                    AccountId = c2.Key.AccountId,
                    SecurityId = c2.Key.SecurityId,
                    TradeType = c2.Key.TradeType.Select(t=>{
                        //
                        // **** Inside Here t is treated as a Character instead of a String
                        //
                        if(t.Equals("NORMAL")) return String.Format("It is a {0} Trade", t); 
                        else if(t.Equals("EQ-NEG")) return String.Format("It is a {0} Trade", t); 
                        else return String.Format("It is a {0} Trade", t); 
                    }).First(),
                    Trades = c2.Count(),
                    Quantity = c2.Sum(s=>s.Qty),
                    Turnover = c2.Sum(s=>s.NetAmount)

                }).ToList()
            }
            ).ToList();

    r.Dump();
}

// Define other methods and classes here
public class Trade
{
    public string AccountId {get;set;}
    public string SecurityId {get;set;}
    public string TradeType {get;set;}
    public int Qty {get;set;}
    public decimal Price {get;set;}
    public decimal NetAmount {get;set;}
}

public class AccountManagerAccountModel
    {
        public string AccountId { get; set; }
        public string AccountName { get; set; }
        public List<AccountStockModel> Securities { get; set; }
    }

    public class AccountStockModel
    {
        public string AccountId { get; set; }
        public string SecurityId { get; set; }
        public string TradeType {get;set;}
        public int Trades { get; set; }
        public int Quantity { get; set; }
        public decimal Turnover { get; set; }
    }

我得到的輸出是:

在此處輸入圖片說明

請參閱 TradeType 列輸出; 它需要一個字符而不是整個字符串。 從技術上講,它應該是正常It is a NORMAL Trade而不是It is a N Trade

更新:

我創建了一個函數並為其傳遞參數。 這解決了問題,而且很干凈。

public string ComposeTradeType(string input)
{
    var output = string.Empty;
    switch(input)
    {
        case "NORMAL":
            output = String.Format("It is a {0} Trade", input);
            break;
        case "EQ-NEG":
            output = String.Format("It is a {0} Trade", input);
            break;
        default:
            output = String.Format("It is a {0} Trade", input);
            break;

    }

    return output;

}

在我的 Linq 聲明中,我是這樣做的:

TradeType = ComposeTradeType(c2.Key.TradeType)
c2.Key.TradeType.Select(t => 

字符串是一個 IEnumerable。 TradeType.Select(t (t) 表示 TradeType 中的每一個字符。因此,Always else 條件被執行。

else return String.Format("It is a {0} Trade", t);

您可以更改以下內容:

 var r = (from t in trades
        group t by new { t.AccountId }
        into g
        select new AccountManagerAccountModel()
        {
            AccountId = g.Key.AccountId,
            AccountName = g.Key.AccountId,
            Securities = g.GroupBy(c1=> new {c1.AccountId, c1.SecurityId, c1.TradeType}).Select(c2=> new AccountStockModel()
            {
                AccountId = c2.Key.AccountId,
                SecurityId = c2.Key.SecurityId,
                TradeType = c2.Key.TradeType.First().Equals("NORMAL") ? "It is a Normal Trade" : c2.Key.TradeType.First().Equals("EQ-NEG") ? "It is a EQ-NEG Trade" : String.Format("It is a {0} Trade", c2.Key.TradeType),
                Trades = c2.Count(),
                Quantity = c2.Sum(s=>s.Qty),
                Turnover = c2.Sum(s=>s.NetAmount)

            }).ToList()
        }
        ).ToList();

您不需要使用 linq,如果 else 循環,您可以使用字符串插值來實現您想要的效果。

TradeType = $"It is a {c2.Key.TradeType} Trade";

我認為使用 linq 沒有任何意義。你只想用 tradetype 創建一個句子。

在這種情況下不使用 linq,為什么不使用 switch case 創建 lambda 函數。

public string CheckTradeType(string tradetype) => 
     tradetype switch
     {
       "NORMAL" => "It is a NORMAL Trade",
       "OTHER"  => "It is a OTHER Trade", 
        ...
        _ => "NOTHING"
     };

並在您的貿易類型變量中

 TradeType = CheckTradeType(c2.Key.TradeType);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM