从索引N到索引M选择人员 [英] Select Persons from Index N to Index M

查看:41
本文介绍了从索引N到索引M选择人员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我有一个Person实体:

Hello
I have a Entity of Person :

var PersonList = new MyEntities().People;



我每次需要显示10条记录.例如第350000至350010

我做了:



I need to show 10 records of them every time. For example 350000th to 350010

I did:

var persons = from person in PersonList
              select person;

foreach (Person person in persons)
{
       if (k >= 350000 && k <= 350010)
           ForShowing.Add(person);

       if (k == 350010)
           break;
       k++;
       MessageBox.Show(person.Name);
}




但是性能不好.

什么是您的解决方案.请帮忙.




But not a good performance.

What''s your solution. please help.

推荐答案

好,例如,您有一个person集合:


Ok, for example you have a collection of person :


List<person> PersonList = new List<person>();


            Person person1 = new Person()
            {
                Name = "John"
            };
            Person person2 = new Person()
            {
                Name = "Bob"
            };
            Person person3 = new Person()
            {
                Name = "Sara"
            };
            Person person4 = new Person()
            {
                Name = "Angel"
            };
            Person person5 = new Person()
            {
                Name = "Bill"
            };

            PersonList.Add(person1);
            PersonList.Add(person2);
            PersonList.Add(person3);
            PersonList.Add(person4);
            PersonList.Add(person5);



现在,您需要从0到3.可以指定发件人"和发件人",以指定您的人员范围:



Now you want from 0 to 3. Ok, can specify ''From'' and ''To'', to have your range of persons:

int fromIndex, toIndex;

            fromIndex = 0;
            toIndex = 3;

            var invertPersons = (from person in PersonList                          
                          select person).Take(fromIndex);

            var persons = ((from person in PersonList                          
                          select person).Except(invertPersons)).Take(toIndex - fromIndex +1);

            
            foreach (Person person in persons)
            {
                Console.WriteLine(person.Name);
            }


Shahin的答案将帮助您解决性能问题,因为他的方法将更加高效.

但是,您可能有更大的根本问题.如果您的人员列表是从数据库中加载的,那么我建议您在db查询端进行分页.您的性能问题是,您要将约300,000条记录加载到集合中,然后在代码中对其执行分页过滤器.这就是为什么您会遇到性能下降的原因.您可以在其中插入开始索引和结束索引或计数的地方使用存储过程或参数化查询.数据库的执行效率和效率要高得多.
Shahin''s answer will help your performance issue as his method will quite simply be more efficient.

However, you may have a bigger root issue. If your list of persons is being loaded from the database, then I would suggest that your paging be done on the db query side. Your performance issue is that you are loading some 300,000 plus records into your collection and then performing a paging filter on it in code. That is why you are experiencing the performance hit. You can use a stored procedure or parametrized query where you inject the start and end indices or counts. The database is far more efficient and quick at doing this.


您应该能够通过索引直接访问列表:

You should be able to access the list directly by index:

List<person> PersonList = new List<person>();
//Add people
int fromIndex, toIndex;
fromIndex = 350000;
toIndex = 350010;
for (int i = fromIndex; i <= toIndex; i++)
{
//Do something
MessageBox.Show(PersonList[i].Name);
}


这篇关于从索引N到索引M选择人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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