linq to sql and background worker

本文关键字:background worker and sql to linq | 更新日期: 2023-09-27 18:01:45

我在后台使用LINQ查询。当我运行程序并执行查询时,它可以正常工作。但是,当我手动更改表中的值并再次运行查询时,这将返回最后一个结果给我,我应该关闭程序并再次运行它以查看更改!

请帮我解决这个问题。

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(); }));
}

linq to sql and background worker

您的DataContext已过期。将构造移动到DoWork方法中:无论如何,DataContext构造是非常轻的,所以它不会对性能造成那么大的损害。

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(); }));
}