最近用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属性。有没有大虾能够指点一下呢?不胜感激!
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属性。有没有大虾能够指点一下呢?不胜感激!
解决方案 »
- datetimepicker 下拉选项
- Infragistics的UltraExplorBar控件,哪儿有单个Items的事件啊?
- 继承DataGridViewColumn的自定义列,在DataGridView的columns中能显示,他用的是什么机制?
- 怎样用dropdownlist和gridview配合做组合查询?
- 8.0是2005还是2008?
- 分不多,求人解决:怎样换个方式弹出右键菜单???
- 关于用C#在XML文件的节点中增加属性的问题
- 如何像设计器一样,用鼠标把控件画到界面上,并可以移动和调整大小(程序在运行时)
- 我想把数据库操作封装成一个类,以这种方式写,那不是很不好设计类库了吗?请教。
- BBclass Derive from Bclass == BBclass inherit Bclass ?
- 关闭窗体右上角那个X 弹出对话框询问,这个怎么实现?
- 带参数的方法与构造函数有什么区别?
显示数据特别慢是因为你直接将LINQ TO SQL的对象赋值给GridControl了,这样做是不对的。你应该使用ToList()方法转换后,缓存到本地再给它加载,这样可以大大提高速度。另外你如果不想要加载某个列,就不应该使用......select cust的方法返回全部列。