268

I have a LINQ query:

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?

1

6 Answers 6

493
var list = (from t in ctn.Items
           where t.DeliverySelection == true && t.Delivery.SentForDelivery == null
           orderby t.Delivery.SubmissionDate
           select t).Take(5);
9
  • 7
    +1, but gah, wrapping multiline expressions in parentheses and then de-referencing the whole lot really grates me for some reason. Jul 29, 2014 at 13:00
  • 11
    This seems to take as many results from the database as match the equality conditions, and only after they are taken from the database does it apply the take(5) restriction within the application. Is there a way to literally take only the first 5 rows from the database?
    – JM Hicks
    Dec 16, 2014 at 8:45
  • 7
    @JMHicks not really. The Take(5) command is only adding yet another condition to the IQueryable, which will not execute until you enumerate it. However, there may be LINQ providers that doesn't support the Take operation. Aug 25, 2015 at 1:59
  • 1
    @JMHicks -- that is not the way linq works... linq is lazy.
    – Hogan
    Feb 28, 2017 at 18:39
  • 2
    @JMHicks According to the documentation at learn.microsoft.com/en-us/dotnet/api/… this construction will NOT enumerate the whole set first. Quote: Take is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach Dec 20, 2021 at 8:20
44

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);
0
27

This can also be achieved using the Lambda based approach of Linq;

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

[Offering a somewhat more descriptive answer than the answer provided by @Ajni.]

This can also be achieved using LINQ fluent syntax:

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. Also note that the documentation for Enumerable.Take begins with:

Returns a specified number of contiguous elements from the start of a sequence.

7

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.

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

Just thinking you might be feel unfamiliar of the sequence From->Where->Select, as in sql script, it is like 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

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.