如何限制返回的数据记录 [英] How do I limit data records returned

查看:63
本文介绍了如何限制返回的数据记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







根据这个问题,我是编程新手。



我想知道如何使用'C#'编写'下拉列表',以便它一次加载并显示100个项目。



< b>问题:< / b>

我的(MS SQL)数据集有40,000条记录 - 'dropdownlist'同时拉动所有行



我尝试过:



SQL存储过程;每日在线搜索拉出我的头发。

解决方案

引用:

我的(MS SQL)数据集有40,000条记录 - 'dropdownlist'同时拉动所有行

一个删除40,000条记录的删除列表的原则本身就是一个坏主意。按100块加载记录不是一个很好的解决方案,因为如果你需要列表末尾的东西,你仍然需要加载40,000条记录,这将增加延迟。



您需要构建一个动态填充的下拉列表,就像Google在您输入内容时所做的那样。

您必须想象一些对您有用的东西。

使用Google,它只会显示与您已键入的内容相匹配的最受欢迎的答案,并且每次键入字母时都会重新生成列表。


在SQL查询中使用TOP语句,例如

  SELECT  < span class =code-keyword> TOP   100  *  FROM  MyTable; 



你也可以使用 SET ROWCOUNT 来限制记录数量,参见

[ ^ ]



但是,使用 LINQ 可能更简单,因为它有 .skip .take 来获取记录。


我假设您正在使用某种ORM,那么什么你可以为你的数据绑定/加载下拉是使用linq。



所以你有类似

的东西

  var  dataFromDb =  new 列表< yourentity>(); 
var data = dataFromDb.Take( 100 );





为了一次加载100个,为了获得下一个100,你需要跟踪当前的数量,然后做一些事情以下几行



  var  currentCount =  2 ; 
var dataSetSize = 100 ;

var dataFromDb = new List< yourentity>();
var data = dataFromDb.Skip(dataSetSize * currentCount).Take(dataSetSize);





你如何跟踪currentCount取决于你,但这是实现你所要求的一般想法。


Hi,


Per the question, I'm new to programming.

I would like to know how using 'C#" to program a 'dropdownlist' so that it will "load and show" a 100 items at a time.

<b>The Problem:</b>
My (MS SQL) data-set has 40,000 records - the 'dropdownlist' pulls all the rows at the same-time

What I have tried:

SQL stored procedures; searched online daily; pulling-out my hair.

解决方案

Quote:

My (MS SQL) data-set has 40,000 records - the 'dropdownlist' pulls all the rows at the same-time

The principle of a droplist that pull 40,000 records is a bad idea by itself. Loading records by chunk of 100 is not a good solution because if you need something at the end of list, you still need to load 40;000 records and it will add delay.

You need to build a dynamically populated droplist just like what Google does when you type something.
You have to imagine something that will work for you.
With Google, it show only the most popular answers that match what you already typed and the list is rebuild every time you type a letter.


Use the TOP statement in your SQL query, e.g.

SELECT TOP 100 * FROM MyTable;


You can also use SET ROWCOUNT to limit the number of records, see
[^]

However, using LINQ might be simpler as this has .skip and .take to fetch records.


Im am assuming you are using an ORM of some sort so what you could do for your data that is bound/loads into the drop down is use linq.

So you'd have something like

var dataFromDb = new List<yourentity>();
var data = dataFromDb.Take(100);



In order to load them 100 at a time, to get the next 100 you'd need to keep track of what the current count it is on and then do something along the following lines

var currentCount = 2;
var dataSetSize = 100;

var dataFromDb = new List<yourentity>();
var data = dataFromDb.Skip(dataSetSize * currentCount).Take(dataSetSize);



How you go about keeping track of currentCount is up to you but this is the general idea to achieve what you are asking.


这篇关于如何限制返回的数据记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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