简体   繁体   English

LINQ查询选择前五名

[英]LINQ query to select top five

I have a LINQ query: 我有一个LINQ查询:

var list = from t in ctn.Items
           where t.DeliverySelection == true && t.Delivery.SentForDelivery == null
           orderby t.Delivery.SubmissionDate
           select t;

How can I modify this query to select just five results from the database? 如何修改此查询以从数据库中仅选择五个结果?

var list = (from t in ctn.Items
           where t.DeliverySelection == true && t.Delivery.SentForDelivery == null
           orderby t.Delivery.SubmissionDate
           select t).Take(5);

The solution: 解决方案:

var list = (from t in ctn.Items
           where t.DeliverySelection == true && t.Delivery.SentForDelivery == null
           orderby t.Delivery.SubmissionDate
           select t).Take(5);

This can also be achieved using the Lambda based approach of Linq; 这也可以使用Linq的基于Lambda的方法来实现;

var list = ctn.Items
.Where(t=> t.DeliverySelection == true && t.Delivery.SentForDelivery == null)
.OrderBy(t => t.Delivery.SubmissionDate)
.Take(5);

[Offering a somewhat more descriptive answer than the answer provided by @Ajni .] [提供比@Ajni提供的答案更具描述性的答案 。]

This can also be achieved using LINQ fluent syntax : 这也可以使用LINQ 流畅的语法实现:

var list = ctn.Items
    .Where(t=> t.DeliverySelection == true && t.Delivery.SentForDelivery == null)
    .OrderBy(t => t.Delivery.SubmissionDate)
    .Take(5);

Note that each method ( Where , OrderBy , Take ) that appears in this LINQ statement takes a lambda expression as an argument. 请注意,此LINQ语句中出现的每个方法( WhereOrderByTake )都将lambda表达式作为参数。 Also note that the documentation for Enumerable.Take begins with: 另请注意, Enumerable.Take的文档以:

Returns a specified number of contiguous elements from the start of a sequence. 从序列的开头返回指定数量的连续元素。

Additional information

Sometimes it is necessary to bind a model into a view models and give a type conversion error . 有时需要将模型绑定到视图模型中并给出类型转换错误 In this situation you should use ToList() method. 在这种情况下,您应该使用ToList()方法。

var list = (from t in ctn.Items
       where t.DeliverySelection == true && t.Delivery.SentForDelivery == null
       orderby t.Delivery.SubmissionDate
       select t).Take(5).ToList();

Just thinking you might be feel unfamiliar of the sequence From->Where->Select, as in sql script, it is like Select->From->Where. 只是觉得你可能不熟悉序列From-> Where-> Select,就像在sql脚本中一样,它就像Select-> From-> Where。

But you may not know that inside Sql Engine, it is also parse in the sequence of ' From->Where->Select ', To validate it, you can try a simple script 但你可能不知道在Sql Engine里面,它也按照' From-> Where-> Select '的顺序进行解析,要验证它,你可以尝试一个简单的脚本

select id as i from table where i=3

and it will not work, the reason is engine will parse Where before Select , so it won't know alias i in the where . 而且它不会工作,原因是引擎会分析其中 选择之前,所以不会知道别名我在哪里 To make this work, you can try 为了使这项工作,你可以尝试

select * from (select id as i from table) as t where i = 3

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

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