将值从模型传递到第二个模型

时间:2018-07-31 23:15:55

标签: c# model

我有两个模型:

public class StatisticsModel
{
    public int? ProgressToBackCheck { get; set; }
    public int? BackcheckToCorrection { get; set; }
    public int? CorrectionsToCompleted { get; set; }
    public int? ProgressToCompleted { get; set; }
}

public class AssignStatisticsModel
{
    public string TasksName { get; set; }
    public int Time { get; set; }
}

我将信息添加为第二个模型:

 List<AssignStatisticsModel> textModels = new List<AssignStatisticsModel>();
 AssignStatisticsModel textmodel = new AssignStatisticsModel();
        textmodel.TasksName = "Progress to Back Check";
           textModels.Add(textmodel);
        textmodel.TasksName = "Back Check to Corrections";
           textModels.Add(textmodel);
        textmodel.TasksName = "Corrections to Completed";
           textModels.Add(textmodel);
        textmodel.TasksName = "Progress to Completed";
           textModels.Add(textmodel);

因此您可以看到我有4行,但列Time为空,我想用我的StatisticsModel信息填充它:

List<StatisticsModel> models = new List<StatisticsModel>();

foreach (var i in tasks)
{
    StatisticsModel model = new StatisticsModel();

    var timeOfTasks = db.GetTableBySQL($"exec usp_Get_WorkedProyectTime @TaskTypeCategoryId = '{i.TaskTypeCategoryId}', @TaskId= '{i.TaskId}'");

    var progressToBackCheck = (from DataRow dr in timeOfTasks.Rows where timeOfTasks.Columns.Contains("ProgressToBackCheck") select dr["ProgressToBackCheck"]).FirstOrDefault();
    var backCheckToCorrections = (from DataRow dr in timeOfTasks.Rows  where timeOfTasks.Columns.Contains("BackCheckToCorrections") select dr["BackCheckToCorrections"]).FirstOrDefault();
    var correctionsToCompleted = (from DataRow dr in timeOfTasks.Rows where timeOfTasks.Columns.Contains("CorrectionsToCompleted") select dr["CorrectionsToCompleted"]).FirstOrDefault();
    var progressToCompleted = (from DataRow dr in timeOfTasks.Rows  where timeOfTasks.Columns.Contains("ProgressToCompleted") select dr["ProgressToCompleted"]).FirstOrDefault();

    model.ProgressToBackCheck = (int?)progressToBackCheck;
    model.BackcheckToCorrection = (int?)backCheckToCorrections;
    model.CorrectionsToCompleted = (int?)correctionsToCompleted;
    model.ProgressToCompleted = (int?)progressToCompleted;

    models.Add(model);
}

StatisticsModel masterSet = models[0];
StatisticsModel leadLog = models[1];
StatisticsModel takeOff = models[2];
StatisticsModel consolidation = models[3];
StatisticsModel design = models[4];
StatisticsModel agreement = models[5];
StatisticsModel shops = models[6];
StatisticsModel hardSpec = models[7];

因此您可以看到,我也有4行,如masterSet或leadLog。我想知道如何获取它们并将其添加到我的AssignStatisticsModel列Time中,是否可以与StatisticsModel进行一些合并并将值传递给AssignStatisticsModel?问候

我尝试将它们分配给foreach,例如:

foreach (StatisticsModel m in masterSet)
{

}

但是我明白了

  

foreach语句不能对类型为变量的变量进行操作   “ StatisticsModel”,因为“ StatisticsModel”不包含公共   GetEnumerator的实例定义

为清楚起见,我举了一个例子

masterSet值

enter image description here

当前的AssignStatisticsModel

enter image description here

所以我想要获得ProgressToBackCheck的值masterset并传递到AssignStatisticsModel的时间[0],获得BackcheckToCorrection的值mastersetAssignStatisticsModel中的时间1等等等

我尝试这样的事情:

foreach (var t in textModels)
            {
                if(t.TasksName.Equals("Progress to Back Check"))
                {
                    textmodel.Time = (int)masterSet.ProgressToBackCheck;
                    textModels.Add(textmodel);
                }
                if(t.TasksName.Equals("Back Check to Corrections"))
                {
                    textmodel.Time = (int)masterSet.BackcheckToCorrection;
                    textModels.Add(textmodel);
                }
                if (t.TasksName.Equals("Corrections to Completed"))
                {
                    textmodel.Time = (int)masterSet.CorrectionsToCompleted;
                    textModels.Add(textmodel);
                }
                if (t.TasksName.Equals("Progress to Completed"))
                {
                    textmodel.Time = (int)masterSet.ProgressToCompleted;
                    textModels.Add(textmodel);
                }
            }

但是它将所有对象更改为模型,并在第二轮的foreach中返回:

  

System.InvalidOperationException:'集合已被修改;   枚举操作可能无法执行。'

0 个答案:

没有答案