简体   繁体   English

如何从Linq中的datetime列获取Month Name到SQL?

[英]How to get Month Name from datetime column in Linq to SQL?

I have a DateTime column in my table. 我的表中有一个DateTime列。

In Linq to SQL query, I only need Month part of that field 在Linq to SQL查询中,我只需要该字段的Month部分

How to write the Linq query for that? 如何编写Linq查询?

Well, there are three options I can immediately think of: 好吧,我可以立即想到三个选项:

  • Pull the whole DateTime back, and then extract the month and format it client-side 拉回整个DateTime ,然后提取月份并将其格式化为客户端
  • Use DateTime.Month to pull just the month back from the database, then format it client-side 使用DateTime.Month从数据库中提取月份,然后将其格式化为客户端
  • Try to get the formatting (conversion to text) to work on the database 尝试获取格式(转换为文本)以在数据库上工作

Personally I would not try the third option - it feels to me like it's very unlikely to work, at least without a lot of effort. 就个人而言,我不会尝试第三种选择 - 我觉得它不太可能工作,至少没有多少努力。

I suspect I'd actually just pull the DateTime back, and do everything client-side. 我怀疑我实际上只是将DateTime拉回来,并做客户端的一切 It's very easy to do that, and it's bound to work. 这样做非常容易,而且必然会起作用。 I expect DateTime.Month will probably work, but you're not going to be saving very much network bandwidth, and you'll be getting the server to do work which is probably just as easy for the client to do. 我希望DateTime.Month 可能会工作,但你不会被节省非常多的网络带宽,而且你会得到服务器做的工作,这可能只是为方便客户做的。

Note that if you still want the results as a query, you can use AsEnumerable to force the remainder of the query to be done client-side. 请注意,如果您仍希望将结果作为查询,则可以使用AsEnumerable强制查询的其余部分在客户端完成。 For example: 例如:

var query = db.Customers
              .Where(c => c.OrderCount > 500) // Or whatever query you want
              .Select(c => new { c.Name, c.FirstOrder })
              .AsEnumerable() // Do the rest of the query on the client
              .Select(c => new { c.Name, c.Month = c.FirstOrder.ToString("MMM") });

EDIT: As noted in comments, it makes sense to use DateTime.Month on the server side if it will actually affect the results , eg 编辑:如评论中所述,如果它实际上会影响结果 ,则在服务器端使用DateTime.Month是有意义的,例如

var query = db.Customers.Where(c => c.FirstOrder.Month == 1)
                        ...

... but in those cases I'd expect it to be the numeric value which is important, not the name of the month. ...但在这些情况下,我希望它是重要的数值,而不是月份的名称

Something like: 就像是:

string monthName = dataContext.GetTable<TableName>()
                              .FirstOrDefault(t => t.DateTimeColumn)
                              .ToString("MMM");               

You need to convert your table to Enumerable type by using ToList() , because you can't use ToString() on query type. 您需要使用ToList()将表转换为Enumerable类型,因为您不能在查询类型上使用ToString()

var result = from sales in SalesOrderHeaders.ToList()
    select new {
        Sales_Order_ID = sales.SalesOrderID,
        Territory_ID = sales.TerritoryID,
        Month = sales.OrderDate.ToString("MMMM"),
        Year = sales.OrderDate.Year
};
result.Dump();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM