hiFI
hiFI

Reputation: 1961

Condition inside a Lambda Expression

I was learning to have condition inside my lambda expression. For such I look into various examples of others who have asked the same.

I was able to get a working result set. But what I witnessed is that at the condition of TradeType, the lambda is treating as a character instead of string and the result returns as char.

My working in LinqPad is as follows:

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

and the output I am getting is as:

enter image description here

Refer the TradeType column output; It takes a single character and not the whole string. Technically it should be It is a NORMAL Trade instead of It is a N Trade

Update:

I made a function and pass the argument for it. This solved the problem and its clean.

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;

}

And in my Linq statement I did like this:

TradeType = ComposeTradeType(c2.Key.TradeType)

Upvotes: 0

Views: 109

Answers (3)

phonemyatt
phonemyatt

Reputation: 1377

You don't need to use linq and if else loop you can just use string interpolation to achieve what you want like this.

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

I don't see any point using linq.you just want to create a sentence with tradetype.

instead of using linq in this case, why not create lambda function with switch case.

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

and in your tradetype variable

 TradeType = CheckTradeType(c2.Key.TradeType);

Upvotes: 0

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30545

c2.Key.TradeType.Select(t => 

string is an IEnumerable. TradeType.Select(t (t) denotes every single character in TradeType. Thus, Always else condition is executed.

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

Upvotes: 1

Gauravsa
Gauravsa

Reputation: 6514

You can change the following:

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

Upvotes: 1

Related Questions