Linq-To-SQL查询是否返回新实例引用? [英] Linq-To-SQL Query Returns New Instance Reference?

查看:49
本文介绍了Linq-To-SQL查询是否返回新实例引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个使用Linq-to-SQL来访问SQL Server数据库的应用程序/项目.对应用程序使用的SQL视图进行了微小更改(重新命名了几列)后,我将视图从Server Explorer拖放到Data Context中以重新捕获,以捕获这些更改-并破坏了到那时为止效果一直很好的应用程序.为了解决此问题,我创建了一个测试应用程序,它表现出相同的行为.生产和测试应用程序之间的通用性是SQL Server数据库视图.

WPF DataGrid用于显示针对View的Linq查询的结果:

I inherited an application/project that employs Linq-to-SQL for its access to a SQL Server database. After making a minor change to a SQL View (renaming a couple of the columns) used by the application, I dropped and re-dragged the view from the Server Explorer into the Data Context to capture those changes -- and broke a feature of the app that had worked beautifully up until then. To try to resolve the matter, I created a test app and it manifests the same behavior; the commonality between the production and test apps is the SQL Server database View.

A WPF DataGrid is used to display the results of a Linq query against the View:

private void GetOrders()
{
    DataClasses1DataContext dataContext = new DataClasses1DataContext() { ObjectTrackingEnabled = false };

    var query = from wrk in dataContext.OrdersListViews
                where wrk.WorkstationID == "WK?"
                   && wrk.OrderConsolidateStatusID != "999"
                select wrk;

    dgrdOrders.ItemsSource = null;
    dgrdOrders.ItemsSource = query;
}



到目前为止没有问题.现在是折断的部分,在其中一些网格列中搜索指定的值,如果找到,则尝试选择该值:



No problem so far. The now-broken piece is this, where a specified value is searched for in some of the grid''s columns and, if found, is attempted to be selected:

 private void FindOrder(string pSearchFor)
 {
     // Ensure that there is a list to select something from!
     IQueryable<OrdersListView> displayedOrders = dgrdOrders.ItemsSource as IQueryable<OrdersListView>;
     if (displayedOrders == null)
         return;

     IQueryable<OrdersListView> query = displayedOrders.Where(x => x.ItemNo == pSearchFor);
     if (query.Count() < 1)
     {
         query = displayedOrders.Where(x => x.OrderNo == pSearchFor);
         if (query.Count() < 1)
         {
             query = displayedOrders.Where(x => x.PickListHdr == pSearchFor);
             if (query.Count() < 1)
             {
                 query = displayedOrders.Where(x => x.PickListNo == pSearchFor);
                 if (query.Count() < 1)
                 {
                     query = displayedOrders.Where(x => x.PutAwayNo == pSearchFor);
                 }
             }
         }
     }

     if (query != null
     &&  query.Count() > 0)
     {
         Debug.Print("Found it!");
         OrdersListView olv = query.First();
         dgrdOrders.SelectedItem = olv;
         dgrdOrders.ScrollIntoView(olv);
     }
     else
     {
         Debug.Print("Entry not found!");
     }
}



实际上,所需的订单已定位并分配给了dgrdOrders.SelectedItem-该项目只是没有在网格中突出显示(选中)!这曾经工作!显然,必须发生的是olv不包含对该订单相同实例的引用,但是怎么回事呢?以及我该如何解决该问题?任何想法将不胜感激.



The desired order is, in fact located and assigned to dgrdOrders.SelectedItem -- the item just doesn''t get highlighted (selected) in the grid! And this used to work! Clearly, what must be happening is that olv does not contain a reference to the same instance of the order but how can this be? And how do I correct the problem? Any ideas would be greatly appreciated.

推荐答案

在您假定实例相同之前,我将检查GetHashCode 以查看对象是否似乎是对象.假设它是不同的实例之前相同.如果覆盖了GetHashCode:

Before you assume that the instances are the same, I would check the GetHashCode to see if the object appears to be the same before assuming that it is a different instance. If overridden the GetHashCode:

// Assign the address of number to a pointer:
int* p = &number;



我也对这一行感到有些困惑:



Also I am a little puzzled by the line:

OrdersListView olv = query.First();


这篇关于Linq-To-SQL查询是否返回新实例引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆