如何使用 RIA 服务将 SQL 查询转换为 LINQ?

发布于 2024-11-25 01:14:07 字数 1063 浏览 2 评论 0原文

我从一个针对视图和视图进行选择的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-12-02 01:14:07

看到这个问题

实体不能在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文