使用 LINQ 从列中获取各个日期的最佳方法

发布于 2024-11-27 21:43:59 字数 454 浏览 0 评论 0原文

我在一个 MSSQL 表列中有以下数据

2011-06-20 11:53:32.000
2011-06-20 11:54:24.000
2011-06-20 11:55:45.000
2011-08-05 10:24:12.000
2011-08-05 10:25:28.000
2011-08-05 10:26:20.000
2011-08-05 10:27:12.000
2011-08-05 10:28:04.000
2011-08-05 10:28:55.000

使用 LINQ,我想从该列中获取以下数据

2011-06-20 
2011-08-05

所以我可以将其放入 List<> 中

最好的方法是什么?我已经设置了上下文内容,所以我不需要这方面的详细信息。我只需要了解可以用来获取这些数据的最佳“查询”和逻辑。谢谢!

I have the following data in one MSSQL Table column

2011-06-20 11:53:32.000
2011-06-20 11:54:24.000
2011-06-20 11:55:45.000
2011-08-05 10:24:12.000
2011-08-05 10:25:28.000
2011-08-05 10:26:20.000
2011-08-05 10:27:12.000
2011-08-05 10:28:04.000
2011-08-05 10:28:55.000

Using LINQ, I would like to get the following data from the column

2011-06-20 
2011-08-05

So I can put into a List<>

What's the best way to do this? I already have the context stuff setup, so I don't need details on that. I just need an idea of the best "query" and logic I can use to get this data. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红焚 2024-12-04 21:43:59

您正在查找一系列日期时间中的所有不同日期,下面将显示示例来获取您需要的内容,前提是您已经将它们放在字符串中或通过查询数据库来获取:

示例(包含字符串列表)

var list; //Contains your dates
var result = list.Select(x => x.Date.ToShortDateString()).Distinct();

示例(来自数据库表)

List<String> dates = (FROM d in datesTable
                      SELECT d.date.ToShortDateString()).Distinct().ToList();

演示功能的控制台示例:

List<DateTime> list = new List<DateTime>();
list.Add(DateTime.Parse("2011-06-20 11:53:32.000"));
list.Add(DateTime.Parse("2011-05-20 11:53:32.000"));
list.Add(DateTime.Parse("2011-05-20 11:44:32.000"));
list.Add(DateTime.Parse("2011-04-20 11:53:32.000"));

var result = list.Select(x => x.Date.ToShortDateString()).Distinct();

foreach (string date in result)
{
    Console.WriteLine(date);
}
Console.Read();

You are looking for all of the Distinct dates in a range of DateTimes, the following will show examples to get what you need, given you already have them in a string or by querying your database:

Example (with list of strings):

var list; //Contains your dates
var result = list.Select(x => x.Date.ToShortDateString()).Distinct();

Example (from database table):

List<String> dates = (FROM d in datesTable
                      SELECT d.date.ToShortDateString()).Distinct().ToList();

Console Example to demonstrate functionality:

List<DateTime> list = new List<DateTime>();
list.Add(DateTime.Parse("2011-06-20 11:53:32.000"));
list.Add(DateTime.Parse("2011-05-20 11:53:32.000"));
list.Add(DateTime.Parse("2011-05-20 11:44:32.000"));
list.Add(DateTime.Parse("2011-04-20 11:53:32.000"));

var result = list.Select(x => x.Date.ToShortDateString()).Distinct();

foreach (string date in result)
{
    Console.WriteLine(date);
}
Console.Read();
困倦 2024-12-04 21:43:59
var q = from t in mydates select distinct ( t.Date.ToShortDateString());
var q = from t in mydates select distinct ( t.Date.ToShortDateString());
盛夏尉蓝 2024-12-04 21:43:59

要在列表中选择不同的日期,请尝试以下操作:

List<String> myDates = (from t in myTable
                        select t.myDate.ToShortDateString()
                       ).Distinct().ToList()

To select distinct dates into a list try this:

List<String> myDates = (from t in myTable
                        select t.myDate.ToShortDateString()
                       ).Distinct().ToList()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文