c# - 用方法的输出替换文本框中的任何索引
问题描述
我为列表中的每个人设计了一个带有一些索引的消息框架。像下面这样:
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;
}
你能帮我吗我该怎么办
解决方案
我要教你钓鱼。
使用模板构建字符串有两种主要方法 - 格式化和插值。
选项一:使用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}°.";
所以,你有一个模型——你收集的数据——和一个格式字符串。将它们与这些选项之一结合起来以生成最终的输出字符串。
推荐阅读
- list - 用于处理整数元组的 Haskell 函数
- sql-update - 尝试从 Power BI 中更新 SQL 表
- mobile - 使用文本单击按钮出现在按钮上
- r - 如何在具有多个变量的 ggplot 图中绘制图例?
- json - 如何在 Python3 中从 json 文件中提取坐标
- uml - 如何在用例图中描绘可见性
- python - 如何使用 PyODBC 插入和更新?
- python - Pyinstaller/Pyarmor 索引错误
- javascript - discord.js 中的 GuildMemberManager.resolve() 和 Guild.member() 有区别吗?
- sql - Postgres 约束参考