I am using a LINQ query in backgroundworker. When I run the program and execute the query it works properly. But when I change a value in table manually and run the query again this return last result to me and I should close the program and run it again to see the changes!

Please help me to resolve this problem.

clockEntities objDb = new clockEntities();
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) 
{
       var inOutList = (from may in objDb.Taradods
       where may.Date.Contains("2014/02") && may.BarCodeNo == 8011
       select may);
       this.Invoke(new MethodInvoker(delegate() { 
           dataGridView1.DataSource = inOutList.ToList(); }));
}
有帮助吗?

解决方案

Your DataContext is stale. Move the construction into the DoWork method: DataContext construction is very light anyway, so it will not harm performance that much.

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)  
{
    //==> Move it here     clockEntities objDb = new clockEntities();

    var inOutList = (from may in objDb.Taradods
    where may.Date.Contains("2014/02") && may.BarCodeNo == 8011
    select may);

    this.Invoke(new MethodInvoker(delegate() { 
    dataGridView1.DataSource = inOutList.ToList(); }));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top