首页 > 解决方案 > 用方法的输出替换文本框中的任何索引

问题描述

我为列表中的每个人设计了一个带有一些索引的消息框架。像下面这样:

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

我写了一个方法,它返回所有不同类型的数量并将方法的输出插入到一个列表中。我想用正确的数量替换每个文本框架项目。

例如下面的列表输出:

销售 拒绝金额 伤害量
1230 56555 79646354

我的方法如下:

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

你能帮我吗我该怎么办

标签: c#listmethodsreplacelinq-to-sql

解决方案


我要教你钓鱼。

使用模板构建字符串有两种主要方法 - 格式化和插值。

选项一:使用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°."

选项二:使用 C# 6字符串插值

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}°.";

所以,你有一个模型——你收集的数据——和一个格式字符串。将它们与这些选项之一结合起来以生成最终的输出字符串。


推荐阅读