简体   繁体   English

是否可以使用LINQ(实体)执行此T-SQL查询

[英]Is It Possible to do this T-SQL Query with LINQ (to Entities)

I'm trying to write the following LINQ-Entities query: 我正在尝试编写以下LINQ-Entities查询:

Get a list of Questions, that have been answered, ordered by most recently answered 获取已回答的问题列表,按最近回答的顺序排序

So, basically it's a 1..* between Question and Answer. 所以,问题和答案之间基本上是1 .. *。

So i tried to write the query in SQL first, so that i understood it, and here's what i came up with: 所以我首先尝试在SQL中编写查询,以便我理解它,这就是我想出的:

WITH [Answers] AS
(
    SELECT      QuestionId,
                CreatedOn,
                ROW_NUMBER() OVER 
                (                        
                    PARTITION BY QuestionId
                    ORDER BY    CreatedOn DESC
                ) As [Rank]

    FROM    dbo.Answers
)

select a.*
from dbo.questions a
inner join answers on a.questionid = answers.questionid
where answers.rank = 1
order by answers.createdon desc

Now, i have no idea if it's even possible to do this with LINQ. 现在,我不知道是否可以用LINQ做到这一点。

Of course, that query above might be the wrong way to go about it, so don't think of this as a simple T-SQL to LINQ-Entities translation. 当然,上面的查询可能是错误的方法,因此不要将其视为简单的T-SQL到LINQ-Entities转换。

I'm just looking for a way to write a LINQ-Entities query for the above requirement. 我只是在寻找一种方法来为上述要求编写LINQ-Entities查询。

Any ideas? 有任何想法吗?

EDIT 编辑

Here's what i've tried so far: 这是我到目前为止所尝试的:

var query = questions
              .Where(q => q.Answers.Any())
              .OrderByDescending(
                q => q.Answers.OrderByDescending(
                  a => a.CreatedOn).FirstOrDefault());

Just hopeful i guess. 我猜是有希望的。 Following error received: 收到以下错误:

DbSortClause expressions must have a type that is order comparable. DbSortClause表达式必须具有可比较的类型。 Parameter name: key 参数名称:key

EDIT 编辑

I should also mention that i need to eager load the Answers in the final result set, eg: 我还要提一下,我需要在最终结果集中急切加载 Answers ,例如:

return ctx.Questions.Include(q => q.Answers)
from question in context.Questions
where question.Answers.Any()
let max = question.Answers.Max(a=>a.CreatedOn)
orderby max descending
select question

EDIT: since you want to eager load answers; 编辑:因为你想急切加载答案; you might want to either resort to doing this entire query in SQL Server and exposing it to EF as a stored procedure or you might want to add LastAnswerOn column in questions table. 您可能希望在SQL Server中执行此完整查询并将其作为存储过程公开给EF,或者您可能希望在问题表中添加LastAnswerOn列。 This will make your query much more efficient and simple and you will be able to use it in EF without problems. 这将使您的查询更加高效和简单,您将能够在EF中使用它而不会出现问题。

Replace that FirstOrDefault which does not make sense with First. 用First替换那个没有意义的FirstOrDefault。 Or Replace entire sub-query with Max (on date). 或者用Max(日期)替换整个子查询。 It should work. 它应该工作。

Good luck with EF, it is so broken ;-) In many cases where Linq To Sql would work, with EF I had to give up and fetch the data and do the computation locally. 好运与EF,它是如此破碎;-)在许多情况下,Linq To Sql将工作,使用EF我不得不放弃并获取数据并在本地进行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM