如何使用 RIA 服务将 SQL 查询转换为 LINQ?
我从一个针对视图和视图进行选择的 SQL 查询开始。看起来像这样:
SET @id = ...
SET @variableDate = ...
SELECT Id,
dbo.fnGreaterDateTime(ViewDate,@variableDate) AS GreaterDate,
FROM vwExample
WHERE Id = @id
SQL 函数 fnGreaterDateTime
按您的预期工作,返回传入的两个值中较大的一个。
在使用 EF & 时,我无法将其转换为 LINQ 查询。 RIA 服务。在我的域服务中,我希望能够执行如下操作:
public IQueryable<ExampleViewResult> GetExampleViewResults(int id, DateTime variableDate)
{
var query = from r in this.ObjectContext.ExampleViewResults
where r.Id == id
select new ExampleViewResult
{
Id = r.Id,
ViewDate = (r.ViewDate > variableDate) ? r.ViewDate : variableDate
}
return query;
}
但是当我调用此方法时,我收到一条错误,指出“无法在 LINQ to Entities 查询中构造实体或复杂类型 ExampleViewResult”。
我尝试遵循this 线程,但是当我让域服务方法返回 DTO 列表时,自动代码生成不包括在我的域上下文中使用的方法。
有什么想法吗?
I'm starting with a SQL query that selects against a view & looks like this:
SET @id = ...
SET @variableDate = ...
SELECT Id,
dbo.fnGreaterDateTime(ViewDate,@variableDate) AS GreaterDate,
FROM vwExample
WHERE Id = @id
The SQL function fnGreaterDateTime
works as you would expect, returning the greater of the two values passed in.
I'm having trouble converting this to a LINQ query when using EF & RIA services. In my Domain Service, I would like to be able to do something like the following:
public IQueryable<ExampleViewResult> GetExampleViewResults(int id, DateTime variableDate)
{
var query = from r in this.ObjectContext.ExampleViewResults
where r.Id == id
select new ExampleViewResult
{
Id = r.Id,
ViewDate = (r.ViewDate > variableDate) ? r.ViewDate : variableDate
}
return query;
}
But when I call this method I receive an error that says "The entity or complex type ExampleViewResult cannot be constructed in a LINQ to Entities query".
I tried following some advice over in this thread, but when I have the domain service method return a list of DTOs instead, the auto-code generation doesn't include the method for use in my domain context.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看到这个问题
实体不能在LINQ to Entities 查询,
您只需投影到匿名类型或 DTO
see this question
The entity cannot be constructed in a LINQ to Entities query
you just need to project to an anonymous type or a DTO