将表值设置为等于其他表中的值

时间:2022-11-09 13:08:45

I have one table which functions as a viewModel. From this, it is supposed to be the source of my binding in my view. My object is to update this table/viewModel based on two other tables. I have one property from each of the table checklistTable and AnswerTable that is to be set in my viewModel checkViewModel.

我有一个表作为viewModel。从这一点来看,它应该是我视图中绑定的来源。我的目标是基于另外两个表更新此表/ viewModel。我在每个表checklistTable和AnswerTable中都有一个属性,它将在我的viewModel checkViewModel中设置。

At the moment I am querying the current elements I need from each of the tables, and trying to update the viewModel.

目前我正在查询每个表中需要的当前元素,并尝试更新viewModel。

CodeBehind:

//Curent descriptions to be set in the viewModel
var currentDescription = (_check.Where(s => currentElements.Contains(s.defineId)).Select(s=> s.Description).ToList());

//Current colors to be set in the viewModel
var currentColors = (from current in _answer
                    where current.questionId == currentCheckId && 
                    current.buildingId == currentBuildingId
                    orderby current.dateReported descending
                    select current.backgroundColor).ToList();

After retrieving theese values, i try to update my viewModel, and this is where things go wrong:

检索theese值后,我尝试更新我的viewModel,这就是出错的地方:

for (int i =0 ; i < currentDescription.Count() - 1; i++)
{
  currentViewTable.Description = currentDescription[i];
}
for (int i =0 ; i < currentColors.Count() - 1; i++)
{
    currentViewTable.backgroundColor.Insert(i,currentColors[i]);
}

I get the error: reference not set to an instance of an object. Are there better ways to update my viewModel, of or any tips on what I am doing wrong?

我收到错误:引用未设置为对象的实例。是否有更好的方法来更新我的viewModel,或者我做错的任何提示?

1 个解决方案

#1


0  

I would first recommend to use a foreach loop instead of a regular for loop. I find it much easier to use foreach when working with lists, this will eliminate any clear "off by one" errors usually.

我首先建议使用foreach循环而不是常规for循环。我发现在使用列表时使用foreach要容易得多,这通常会消除任何明显的“一个一个”错误。

foreach (var description in currentDescription)
{
    //do something given each element in the list
}

This seems to be a data integrity issue from what I can gather just looking at your code and given what the error is stating. I would recommend going into debug mode and looking in the Lists that you have generated to see if there are any indexes where the objects in your two lists are null, and handle these instances accordingly.

这似乎是一个数据完整性问题,我可以收集只是查看您的代码并给出错误说明的内容。我建议进入调试模式并查看您生成的列表,以查看是否存在两个列表中的对象为空的任何索引,并相应地处理这些实例。

#1


0  

I would first recommend to use a foreach loop instead of a regular for loop. I find it much easier to use foreach when working with lists, this will eliminate any clear "off by one" errors usually.

我首先建议使用foreach循环而不是常规for循环。我发现在使用列表时使用foreach要容易得多,这通常会消除任何明显的“一个一个”错误。

foreach (var description in currentDescription)
{
    //do something given each element in the list
}

This seems to be a data integrity issue from what I can gather just looking at your code and given what the error is stating. I would recommend going into debug mode and looking in the Lists that you have generated to see if there are any indexes where the objects in your two lists are null, and handle these instances accordingly.

这似乎是一个数据完整性问题,我可以收集只是查看您的代码并给出错误说明的内容。我建议进入调试模式并查看您生成的列表,以查看是否存在两个列表中的对象为空的任何索引,并相应地处理这些实例。