Search code examples
c#listmethodsreplacelinq-to-sql

Replace any index in a text frame with output of a method


I design a frame for message with a some index in it for each person in list. like the one bellow:

  Dear {0} 
  Hi, 
  the total amount of Draft is {1}.
  amount of prm is {2}
  yesterday amount is {3} 

I wrote a method witch return all different type of amount and insert the out put of method in a list . I want to replace each item of text frame with the correct amount .

for example the output like the list bellow :

sale reject amount damage amount
1230 56555 79646354

my method like bellow :

     public List<outputList1> listAmount()
    {

        var amounts = (from p in db.FactTotalAmount
                       
                       group p by p.FromDate  into g
                         select new outputList1
                         {

                             YesterdaySalesPrm = g.Sum(x => 
                               x.YesterdaySalesPrm),
                             YesterdayDraftAmount = g.Sum(x => 
                              x.YesterdayDraftAmount),
                             PrmSales = g.Sum(x => x.PrmSales),
                             DraftAmount = g.Sum(x => x.DraftAmount)
                         }).ToList();

        return amounts;
    }

would you please help me what should I do


Solution

  • I'm going to teach you to fish.

    There are two main ways to build a string using a template - formatting and interpolation.

    Option one: use string.Format:

    string output = string.Format("Today is {0}. Weather is {1} at {2}°.", "Monday", "rain", 75.2);
    // result is "Today is Monday. Weather is rain at 75.2°."
    

    Option two: use C# 6 string interpolation:

    string dayOfWeek = "Monday";
    string weather = "rain";
    decimal temp = 75.2;
    
    // Notice the "$" at the start of the string literal
    string output = $"Today is {dayOfWeek}. Weather is {weather} at {temp}°.";
    

    So, you have a model - the data you've collected - and a format string. Combine those together with one of these options to produce the final output string.