Linq To Sql-从日期获取星期几的名称 [英] Linq To Sql - Get week day name from date
问题描述
是否可以构造一个查询,使我可以从日期中将星期几的名称检索到单独的列或变量中?
Is it possible to construct a query in which I can retrieve week day name from a date into a separate column or variable?
我知道我可以在.NET端很容易地做到这一点,但希望它可以在查询中出现.
I know I can very easily do this on .NET side but would like it to be in the query.
推荐答案
您还可以使用SqlFunctions ...
You can also use SqlFunctions...
var results=context.Listings
.Select(l=>System.Data.Entity.SqlServer.SqlFunctions.DateName("dw",l.modify_date));
当然,这仅在使用 SQL Server 时有效.跨数据库的方法是使用带有已知日期的EntityFunctions.DateDiff,以获取介于已知日期和已知的先前星期日之间的天数,然后取模7,然后转换为字符串.
Of course, this only works when using a SQL Server. Methods that are cross-database, would be to use EntityFunctions.DateDiff with a known date to get the number of days between whatever and a known prior sunday, then modulus 7, then convert to a string.
这篇关于Linq To Sql-从日期获取星期几的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!