[英]how to use complex sql query in EF 4.1 code first
We have just started using EF 4.1 code first. 我们刚刚开始使用EF 4.1代码。 I have a sql query something like this:
我有这样的SQL查询:
SELECT * FROM TableA
WHERE DATEDIFF(DAY,GETDATE(), TableA.ExpirationDate == x days)
I don't know how to use sql query with EF 4.1 code first set of functions. 我不知道如何将SQL查询与EF 4.1代码的第一组功能一起使用。 One function like this:
像这样的一个功能:
IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
Does anybody has an idea? 有人有主意吗?
Something like this is close to your query: 这样的事情很接近您的查询:
var things = context.TableAEntities
.Where(e => EntityFunctions.DiffDays(DateTime.Now, e.ExpirationDate) == x)
.ToList();
It translates to: 转换为:
SELECT
[Extent1].[Id] AS [Id],
//... more columns
[Extent1].[ExpirationDate] AS [ExpirationDate]
FROM [dbo].[TableA] AS [Extent1]
WHERE (DATEDIFF (day, SysDateTime(), [Extent1].[ExpirationDate])) = @p__linq__0
SysDateTime()
(in SQL Server) is the same as GETDATE()
, it just has a higher precision. SysDateTime()
(在SQL Server中)与GETDATE()
相同,只是精度更高。
Try 尝试
context.Database.SqlQuery<EntityA>("SELECT * FROM TableA
WHERE DATEDIFF(DAY,GETDATE(), TableA.ExpirationDate == x days");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.