返回错误的清单。我可以归还另一个吗?

时间:2014-03-05 11:01:34

标签: c# list mvvm observablecollection

好的,我的WPF应用程序中有一个gridview。我使用MVVM和C#。 gridview充满了数据。 我使用Web服务调用数据。我不直接与数据库通信。我们已经设置了存储过程,因此我无法创建自己的存储过程并将其用于应用程序。

我有2张桌子。 MaintenanceCall和MainCallProblems,维护电话的一个孩子 MaintenanceCall:

[Intrecno] [bigint] IDENTITY(1,1) NOT NULL,
[IntrecnoLandObject] [bigint] NULL,
[CallerName] [varchar](100) NULL,
[CallerTel] [varchar](20) NULL,
[CaptureBy] [bigint] NULL,
[CaptureDate] [datetime] NULL,

Intrecno是PK。 IntrecnoLandObject是一个FK,我将用它来提取数据。

MainCallProblems:

[Intrecno] [bigint] IDENTITY(1,1) NOT NULL,
[IntrecnoCallNo] [bigint] NULL,
[CaptureBy] [bigint] NULL,
[CaptureDate] [datetime] NULL,
[ProblemDescription] [varchar](500) NULL,

IntrecnoCallNo是来自MaintenanceCall的FK。

基本上,用户使用我的表单记录对象的投诉/问题。他选择了对象,其ID存储在内存中并记录一个调用。每次通话都会遇到很多问题。单击“保存”后,将记录该呼叫。数据将插入MaintenanceCall,对象的ID,记录呼叫的用户和时间。然后将问题与用户等一起记录到MainCallProblems中......只需知道所有字段都已填写完毕。

现在我的表单上有一个历史gridview。当用户选择要记录呼叫的对象时,应加载历史记录(如果有)。 这是我的问题。

在我的服务代码的某个地方,我把错误的列表放在某个地方。 让我们说有4个MaintenanceCall记录。每个MainCall在MainCallProblems中都有自己的问题。如果一个电话有两个问题怎么办?这就是我所坚持的。

我的服务代码,用于填充列表并发回:

 public List<MaintenanceCall> GetMaintenance(Dictionary<string, object> propertyValues) //Brings in the selected land object intrecno number
    {
        List<MaintenanceCall> mainCalls = ExecuteDataReader<MaintenanceCall>("spGetMainCall", propertyValues, typeof(MaintenanceCall)); //finds all maintenance calls associated with that land object
        foreach (var instance in mainCalls) //for each specific call
        {
            Dictionary<string, object> paramKey = new Dictionary<string, object>();
            paramKey.Add("IntrecnoCallNo", instance.Intrecno); //now sends the primary key of maintenance call into the child table, MainCallProblems and finds the problems with that key, which is the foreign key 

            List<MainCallProblems> problems = ExecuteDataReader<MainCallProblems>("spGetMainCallProblems", paramKey, typeof(MainCallProblems)); //creates a list of the problems associated with that call. can be one or many

            foreach (var problem in problems) //for each problem with that call, fill out the data
            {
                instance.ProblemDesc = problem.ProblemDescription; // the problem description for each problem call
            }
        }

        return mainCalls; //returns the first list, with the data now stored
    }

如您所见,我在方法参数中获得了landobject的ID。这在存储过程中用于检索MaintenanceCall实例。其中有4个。

然后将每个调用分解为调用中的每个问题,即第一个foreach循环。现在将MaintenanceCall的PK传递给MainCallProblems以查找实际问题。 就像我说的,这个列表应该是5。 因此返回的MaintenanceCall列表缺少一条记录。它填充数据并覆盖一条记录,因为一个列表比另一个小。

我该如何纠正这个问题。我不能使一个列表与另一个列表相同,我得到错误。我不能返回任何其他列表类型。

理想情况下我想要退回:

enter image description here

如您所见,这只是我在SQL中编写的一个快速选择,显示了FK和问题。 2个问题具有相同的FK,但在我的应用程序中,一个将被覆盖,我收到4个结果而不是5个

如果需要进一步的解释,请告诉我,我会相应地进行编辑。

1 个答案:

答案 0 :(得分:0)

得到了它。 我创建了一个相同类型的新列表:List<MaintenanceCall> callList = new List<MaintenanceCall>();

并且在找到数据的第二个循环中,我填充了这个新列表:

callList.Add(new MaintenanceCall()
                {
                    ProblemDesc=instance.ProblemDesc
                });

然后我返回这个包含5个问题的新列表。它有效。