[英]How to TOP, ORDER BY and DISTINCT in the same query with Linq-To-Sql?
How to translate the following query to Linq? 如何将以下查询转换为Linq?
SELECT DISTINCT TOP 10 A.*,A.X+A.Y AS SUMXY
FROM TABLE_A AS A INNER JOIN TABLE_B AS B ON A.ID = B.AID
ORDER BY SUMXY
I don't want to split in two queries. 我不想拆分成两个查询。
Using extension methods and assuming you have a foreign key relationship between Table_A and Table_B so that there is an EntitySet named TableBs on the TableAs table (this would be easier using real table names... sigh). 使用扩展方法并假设您在Table_A和Table_B之间具有外键关系,以便在TableAs表上有一个名为TableBs的EntitySet(使用实际表名会更容易...叹气)。
var query = db.TableAs.Where( a => a.TableBs.Count() > 0 )
.Select( a => new { A = a, SumXY = a.X + a.Y } )
.OrderBy( a => a.SumXY )
.Take( 10 );
This will give you back a collection of anonymous-typed objects having an TableA object named A and a SumXY (an int, presumably). 这将带给您一组匿名类型的对象的集合,这些对象具有一个名为A的TableA对象和一个SumXY(可能是int)。 Alternatively, you could create a real class that holds A's data plus the sum and select objects of this type.
或者,您可以创建一个包含A的数据加上总和的真实类,然后选择这种类型的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.