问题描述
我知道Linq-to-SQL已经死了,但是无论如何,我认为这真的很基础,我只是对魔术Linq-to-SQL在它生成的SQL上做什么和不做什么感到困惑.
I know Linq-to-SQL is dead, but anyway, I think this is really basic, I'm just confused about what magic Linq-to-SQL does and doesn't do with regards to the SQL it generates.
如果我已将表达式树构建为"myPredicate",并且具有以下内容:
If I have built up an Expression tree as "myPredicate", and have something like this:
(from request in DataContext.RequestsTable
select request).Where(myPredicate)
.OrderByDescending(item => item.changeDate)
.Take(10)
它是否可以像以下SQL一样工作:
is it going to work like the following SQL:
SELECT TOP 10 * FROM RequestsTable
WHERE (<<myPredicate equivalent>>)
ORDER BY ChangeDate DESC
在我看来,这很奇怪,因为在示例代码中,.Where()"位于选择"之后. "select","where()"和"orderby()"的相对位置会影响事物吗?
It just seems weird to me because the ".Where()" comes after the "select" in my example code. Does the relative positioning of the "select" and "where()" and "orderby()" affect things?
或者,我可以用sql-esque语法来完成所有操作吗?例如,有什么方法可以在替代语法中使用我的WHERE谓词,诸如此类?
Alternatively, could I do it all in sql-esque syntax? For example, is there some way to use my WHERE predicate in the alternative syntax, something like this?
(from request in DataContext.RequestsTable
where [somehow inject myPredicate]
order by changeDate descending
select request).Take(10)
推荐答案
您在此处有相同的查询,直到完成某些操作以执行查询后,LINQ to SQL才会评估和生成T-SQL (例如.ToList().顺序无关紧要.
You've got the same query there, LINQ to SQL won't evaluate and generate the T-SQL until after you've done something to execute the query (such as a .ToList() for example. The ordering doesn't matter.
实际上,您甚至可以在分配初始查询后添加OrderBy和Where子句.
In fact, you can even add OrderBy and Where clauses after the assignment of the initial query.
例如
var query = (from x in context.RequestsTable
select x);
query = query.AsQueryable().Where(<>);
return query.ToList(); //executes
与以下相同:
return (from x in context.RequestsTable
where <>
select x).ToList(); //executes
与以下相同:
return (from x in context.RequestsTable
selext x).Where(<>).ToList();
我不确定LINQ to SQL是否死机",但是我听说它可能已被卷入ADO实体框架中. LINQ to SQL生成的T-SQL远远优于实体框架!
I'm not sure LINQ to SQL is "dead" however I have heard that it might be being rolled into the ADO Entity Framework. LINQ to SQL's generated T-SQL is far superior to the Entity Framework's!
这篇关于在显式LINQ-to-SQL(C#)中,顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!