最近用DevExpress.XtraGrid.GridControl+Linq To SQL实现一个列表时,发现显示数据特别慢,输出Log后发现在对DevExpress.XtraGrid.GridControl.DataSource赋值时,会访问到数据源的某个IList属性,部分测试代码如下:this.gridControl1.ShowOnlyPredefinedDetails = true;Northwind db = new Northwind();
db.Log = Console.Out;
var LondonCustomers =
    from cust in db.Customers
    where cust.City == "London"
    select cust;
this.gridControl1.DataSource = LondonCustomers;在执行最后一条语句时,除了访问绑定的每个Customer的public属性,之前还会访问到Customer.Orders,导致不必要的数据库读取耗时。未接触过Linq的朋友也可以看下面的示例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.Linq;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            A a1 = new A();
            a1.Key = "a1key";
            a1.Name = "a1name";
            A a2 = new A();
            a2.Key = "a2key";
            a2.Name = "a2name";            List<A> list = new List<A>();
            list.Add(a1);
            list.Add(a2);
            this.gridControl1.DataSource = list;
        }        private class A
        {
            private List<B> bs1 = null;
            public List<B> Bs1
            {
                get{return bs1;}
                set{bs1 = value;}
            }            private List<B> bs2 = null;
            public List<B> Bs2
            {
                get{return bs2;}
                set{bs2 = value;}
            }            private string key=string.Empty;
            public string Key
            {
                get{return key;}
                set{key = value;}
            }            private string name = string.Empty;
            public string Name
            {
                get{return name;}
                set{name = value;}
            }
        }        private class B
        {
            private string name = string.Empty;
            public string Name
            {
                get{return name;}
                set{name = value;}
            }
        }
    }
}
在DevExpress.XtraGrid.GridControl显示每个A对象时,还会访问每个A对象的Bs1属性,但不会访问Bs2属性。有没有大虾能够指点一下呢?不胜感激!

解决方案 »

  1.   

    你在说什么啊,什么叫还会访问每个A对象的Bs1属性,但不会访问Bs2属性,明明都会访问,两个都会访问的未接触过Linq的人也不可能来回答你,所以你没必要瞎举例,我反而看不懂你要说明什么了。
    显示数据特别慢是因为你直接将LINQ TO SQL的对象赋值给GridControl了,这样做是不对的。你应该使用ToList()方法转换后,缓存到本地再给它加载,这样可以大大提高速度。另外你如果不想要加载某个列,就不应该使用......select cust的方法返回全部列。
      

  2.   

    这个新控件我也在玩!就是没有玩太深。不是有Demo吗可以看看里面的示例有没有啊