【问题标题】:Returning wrong list. Can I return the other?返回错误列表。我可以退回另一个吗?
【发布时间】:2014-04-07 09:32:46
【问题描述】:

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

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

[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,我将使用它来提取数据。

MainCall问题:

[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...只要知道所有字段都已填写。

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

在我的服务代码的某处,我在某处放置了错误的列表。 假设有 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
    }

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

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

我该如何纠正这个问题。我无法将一个列表与另一个列表相同,我收到错误。而且我不能返回任何其他列表类型。

理想情况下我想退货:

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

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

【问题讨论】:

    标签: c# list mvvm observablecollection


    【解决方案1】:

    知道了。 我创建了一个相同类型的新列表:List&lt;MaintenanceCall&gt; callList = new List&lt;MaintenanceCall&gt;();

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      相关资源
      最近更新 更多