dataGridView1.DataSource = (from x in db.Employees
where x.FirstName.Contains(textBox1.Text)
select x);
I am using the above expression in my txtSearch_TextChanged
event to filter the results real time. Does this call the Database every time a key is pressed to get the results OR are the results cached in the memory in some way and filtered and returned? Is this is the correct way of doing some real time filter like this in LINQ?
Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
private void txtSearch_TextChanged(object sender, EventArgs e)
{
dataGridView1.DataSource = (from x i开发者_JAVA百科n db.Employees
where x.FirstName.Contains(textBox1.Text)
select x);
}
Yes... if you call that every time a key is pressed, the database is being queried every time a key is pressed.
But it's worse than that - LINQ is having to compile that expression into SQL every time, and that's a significant amount of work, too.
So no - that is not the correct way to do it. You should cache the data on the client first to do the auto complete... or at least only start pulling (and then cache) after two characters or so, so you can limit the results to cache.
精彩评论