我有一个数据表,其中以月份为最后一列。我需要打印月份= CurrentMonth的所有行。例如,本月为3月,下个月为4月,等等。
使用下面的示例数据-如何在SS = CurrentMonth的所有行上打印CN和SS?
我想打印到控制台...即Console.PrintLine();
static void Main(string[] args)
{
DataTable dtData = new DataTable();
dtData.Columns.Add("CN", typeof(string));
dtData.Columns.Add("AN", typeof(string));
dtData.Columns.Add("SN", typeof(string));
dtData.Columns.Add("CE", typeof(string));
dtData.Columns.Add("TSD", typeof(DateTime));
dtData.Columns.Add("TSC", typeof(DateTime));
dtData.Columns.Add("T1S", typeof(DateTime));
dtData.Columns.Add("T1C", typeof(DateTime));
dtData.Columns.Add("T2S", typeof(DateTime));
dtData.Columns.Add("T2C", typeof(DateTime));
dtData.Columns.Add("SS", typeof(string));
dtData.Rows.Add("1111", "TestSN", "email", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "March");
dtData.Rows.Add("2222", "TestSN1", "email1", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "July");
dtData.Rows.Add("3333", "TestSN2", "email2", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "January");
dtData.Rows.Add("4444", "TestSN3", "email3", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "April");
dtData.Rows.Add("5555", "TestSN4", "email4", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "October");
dtData.Rows.Add("6666", "TestSN5", "email5", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "December");
dtData.Rows.Add("7777", "TestSN6", "email6", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "March");
}
答案 0 :(得分:2)
您可以使用DataTable的Select方法仅选择SS列等于本月的行。
var allRows = dt.Select($"SS='{DateTime.Now.ToString("MMMM", CultureInfo.InvariantCulture)}'");
foreach(var r in allRows)
{
Console.WriteLine($"{r["CN"].ToString()}, {r["SS"].ToString()}");
}
输出:
1111, March
7777, March
此外,上面的代码无法编译。看起来应该删除代码中的这一行:
dtData.Columns.Add("CE", typeof(string));
答案 1 :(得分:1)
类似这样的东西:
foreach (DataRow r in dtData.Rows)
{
if (r["SS"].ToString() == DateTime.Now.ToString("MMMM"))
{
Console.WriteLine(r["CN"].ToString() + r["SS"].ToString());
}
}
答案 2 :(得分:1)
您需要的是对数据的简单选择查询:
string expression = $"SS = '{DateTime.Now.ToString("MMMM")}'";
var filteredData = dtData.Select(expression);
foreach(var item in filteredData)
{
Console.WriteLine($"CN: {item["CN"]}, SS: {item["SS"]}");
}
顺便说一句,您的示例还有另一个问题,您忘记了字符串列为4,而在行中插入了3个字符串:
dtData.Rows.Add("1111", "TestSN", "email", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "March");
dtData.Rows.Add("2222", "TestSN1", "email1", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "July");
dtData.Rows.Add("3333", "TestSN2", "email2", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "January");
dtData.Rows.Add("4444", "TestSN3", "email3", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "April");
dtData.Rows.Add("5555", "TestSN4", "email4", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "October");
dtData.Rows.Add("6666", "TestSN5", "email5", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "December");
dtData.Rows.Add("7777", "TestSN6", "email6", "CE", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, "March");