我有一个数据库表,其中包含接收到的文件的信息。其中一列是指定接收文件时间的 DateTime。我需要为收到的文件获取一份包含年份 (MM/YYYY) 的不同月份列表。我需要把它从这张 table 上拿下来。此表中可能有数千条记录,所以我在 Oracle 中的做法是在我的 select 语句中将日期时间格式化为 MM/YYYY 并执行带有 distinct 子句的排序描述。这给了我一份文件收到月份的列表。非常快速和高效。
现在我需要使用 EFv4 执行此操作....这是我在 Oracle 中使用的查询。任何人都知道我如何使用一种 EF 查询方式来翻译它?
select distinct
to_char( i.date_received, 'MMYYYY')) MonthAndYear
from table1
order by MonthAndYear desc
最佳答案
好吧,不要像 Oracle 那样做。像 LINQ 那样做。
var q = (from i in Context.Table1
select new
{
Month = i.date_received.Month,
Year = i.date_received.Year
}).Distinct();
获取DateTime
:
var r = q.AsEnumerable().Select(d => new DateTime(d.Year, d.Month, 1));
关于c# - 在 EF 查询中格式化 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299813/