C# 如何在asp.net中打印包含(n-1)个特殊字符的n个数字?
我有一个datatable,其中包含两列InvoiceNo和InvoiceDate,我想在如下文本框中打印这些值:C# 如何在asp.net中打印包含(n-1)个特殊字符的n个数字?,c#,asp.net,C#,Asp.net,我有一个datatable,其中包含两列InvoiceNo和InvoiceDate,我想在如下文本框中打印这些值: Table Invoice No Invoice Date 001 2/10/2013 006 7/10/2014 009 9/11/2014 我想在一个文本框中打印这个,比如2013年10月2日、2014年10月7日、2014年11月9日的发票号001006009。 现在的问题是如
Table
Invoice No Invoice Date
001 2/10/2013
006 7/10/2014
009 9/11/2014
我想在一个文本框中打印这个,比如2013年10月2日、2014年10月7日、2014年11月9日的发票号001006009。
现在的问题是如何在它们之间插入(,)。我不能这样做 试试这个:-
var result = string.Format("Invlice Number {0} dated on {1} resp.", String.Join(",", tables.Select(x => x.InvoiceNumber.ToString("000"))),
String.Join(",", tables.Select(x => x.InvoiceDate.ToShortDateString())));
为了简单起见,我使用了List而不是dataTable
或者,对于DataTable,请尝试以下操作:-
string output = string.Format("Invoice Number {0} dated on {1} resp.", String.Join(",", (from DataRow dr in tab.Rows
select ((int)dr["InvoiceNumber"]).ToString("000")) ),
String.Join(",", (from DataRow dr in tab.Rows
select ((DateTime)dr["InvoiceDate"]).ToShortDateString())));
您可以混合使用LINQ和String.Join()来实现这一点
因为我们对你的工作一无所知,这里有一些你可能需要的步骤
- 我假设您使用Sql Server,使用
和SqlConnection
来获取这些值SqlCommand
- 使用
为读取行提供SqlDataReader
方法(该方法逐行读取行),并将.Read
变量定义为字符串结果的string
和finalNo
finalDate
- 在while循环中,可以将第一列作为
读取,第二列作为reader[0]
读取,并与reader[1]
和finalNo
字符串连接finalDate
- 之后,您需要
字符串上的最后一个逗号,如李>TrimEnd()
- 然后,您可以在它的
属性中使用Text
李>string.Format
你能再解释一下吗?或者你到目前为止做了什么?我试图通过递归循环实现,但没有成功。Gonul我希望投票,但由于我的分数较低,所以没有成功,但肯定我把它标记为正确答案@没问题。你说得对,投票需要声誉。阅读
var rows = (from DataRow row in table.Rows select row["columnname"].ToString()).ToList();
var commaSeparated = string.Join(", ", rows);
using(SqlConnection con = new SqlConnection())
using(SqlCommand cmd = con.CreateCommand())
string finalNo = null, finalDate = null;
using(SqlDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
}
}
while(reader.Read())
{
finalNo += reader[0].ToString() + ",";
finalDate += reader[1].ToString() + " ,";
}
finalNo = finalNo.TrimEnd(',');
finalDate = finalDate.TrimEnd(',', ' ');
TextBox1.Text = string.Format("Invoice Number {0} dated on {1}",
finalNo,
finalDate);