Click here to Skip to main content
15,897,166 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Find bottom 10 record using entity framework from table

i have table "product" in table thousand records but i want last 10 record using entity framework. please

i tried following code

VB
Dim result As Array = Nothing
        Using context = New abc.Data.abcdEntities
            Dim Data = From Value In context.Product
            If Data.Count() > 0 Then
                result = Data.ToArray
            End If
        End Using
Posted
Comments
Sergey Alexandrovich Kryukov 20-Nov-13 12:20pm    
What is "Data" here? Note that database table don't have "bottom records" because a table does not have one predefined order of data in it. The certain order appears only as a result of a query.
—SA

1 solution

Wow. Looking at your code, you REALLY need to pickup a book on Entity Framework and work through it. You're writing a bunch of unneccessary code that can be done using very simple methods in EF and LINQ.

Database engines don't keep track of the "bottom" or "top" of a table. Records are in no particular order in the table unless the query you use forces it by including an "ORDER BY" clause in your SELECT statement.

Once you have the table sorted by one or more of its fields, then you can use the TOP clause to get the number of records you want from the top of the sorted table. In order to get the bottom, you simply sort the table in reverse order, either ascending or descending depending on your sort key and requirements.

In Entity Framework, the query would look something like:
context.Products.OrderBy(c => c.ColumnName).Take(10)

or reversed:
context.Products.OrderByDescending(c => c.ColumnName).Take(10)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900