使用表中的eninty框架查找底部10条记录 [英] Find bottom 10 record using eninty framework from table

查看:61
本文介绍了使用表中的eninty框架查找底部10条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用表中的实体框架查找底部10条记录



我在表千条记录中有表产品但我想要使用实体框架的最后10条记录。请



i试用以下代码



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

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

推荐答案

哇。查看您的代码,您真的需要获取有关实体框架的书并完成它。您正在编写一堆不必要的代码,可以使用EF和LINQ中非常简单的方法完成。



数据库引擎无法跟踪底部或表的顶部。除非你使用的查询通过在SELECT语句中包含ORDER BY子句来强制它,否则表中的记录没有特别的顺序。



一旦你有了表按一个或多个字段排序,然后您可以使用TOP子句从排序表的顶部获取所需的记录数。为了获得底部,您只需按相反顺序对表进行排序,可以根据您的排序键和要求进行升序或降序。



在Entity Framework中,查询看起来像是:

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)


这篇关于使用表中的eninty框架查找底部10条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆