如何将新项目添加到现有列表c#?

如何将新项目添加到现有列表c#?,c#,asp.net,asp.net-mvc,entity-framework,linq-to-entities,C#,Asp.net,Asp.net Mvc,Entity Framework,Linq To Entities,我上传了一个Excel文件,在阅读该文件后得到了这个结果 var data = from c in excel.Worksheet<QuestionMaster>() select new { c.Questionname, c.Answer1, c.Answer2, c.Answer3, c.A

我上传了一个Excel文件,在阅读该文件后得到了这个结果

 var data = from c in excel.Worksheet<QuestionMaster>()
            select new
            {
              c.Questionname,
              c.Answer1,
              c.Answer2,
              c.Answer3,
              c.Answer4,
              c.CorrectAnswer,
            };
var data=来自excel.Worksheet()中的c
选择新的
{
c、 问题名称,
c、 答复1:,
c、 答复2:,
c、 答复3:,
c、 答复4:,
c、 回答正确,
};
现在,我需要检查上传数据中的任何列不应为null

为此,我的代码是:
问题大师是模型课程

QuestionMaster questions = new QuestionMaster();
QuestionMaster temp = new QuestionMaster();
List<QuestionMaster> ques = new List<QuestionMaster>();//
  foreach (var item in data)
   {
      int i = 0;
     if (item.Questionname == null || item.Answer1 == null || item.Answer2 == null ||   item.Answer3 == null || item.Answer4 == null || item.CorrectAnswer == null)
      {
          if (item.Questionname != null)
           temp.Questionname = item.Questionname.Trim();                            
         else
           temp.Questionname = "Question Name not Found in Uploaded Excel File";                            
         if (item.Answer1 != null)
           temp.Answer1 = item.Answer1.Trim();                            
         else
           temp.Answer1 = "Answer 1 Not Found in Uploaded Excel File";                            
                       \\Some more Couple of If-Else Conditions

ques.Add(temp);
      }
QuestionMaster questions=new QuestionMaster();
QuestionMaster temp=新的QuestionMaster();
List ques=新列表()//
foreach(数据中的var项)
{
int i=0;
如果(item.Questionname==null | | | item.Answer1==null | | item.Answer2==null | | | item.Answer3==null | | item.Answer4==null | | item.CorrectAnswer==null)
{
if(item.Questionname!=null)
temp.Questionname=item.Questionname.Trim();
其他的
temp.Questionname=“上传的Excel文件中未找到问题名称”;
如果(item.Answer1!=null)
temp.Answer1=item.Answer1.Trim();
其他的
temp.Answer1=“上传的Excel文件中未找到答案1”;
\\还有几个If-Else条件
添加问题(临时);
}
现在的问题是:
假设我得到了三个不同的行,其中有一些空列,上面的条件为真

询问添加(临时)运行第二次或第三次时,覆盖之前添加的其他列表项,但列表中添加的项目数保持不变。
这意味着它将用临时文件中的当前数据覆盖整个列表

请告诉我哪里出错了

提前感谢

if (ques.Contains(temp))
{
    //todo replace item
}
else
{
    ques.Add(temp);
}
并在循环中重新创建引用


并在循环中重新创建引用

每次都需要在循环中创建一个新对象,当前它在循环之外,因此原始对象会不断更改

  foreach (var item in data)
   {
     QuestionMaster temp = new QuestionMaster();

   }

每次都需要在循环中创建一个新对象,当前它在循环之外,因此原始对象会不断更改

  foreach (var item in data)
   {
     QuestionMaster temp = new QuestionMaster();

   }

您的列表包含QuestionMaster的对象:

List<QuestionMaster> ques = new List<QuestionMaster>();
List ques=新列表();

数据列表具有匿名类型的对象,请尝试在数据中创建QuestionMaster对象,而不是任何匿名类型的对象。

您的列表包含QuestionMaster的对象:

List<QuestionMaster> ques = new List<QuestionMaster>();
List ques=新列表();

数据列表具有匿名类型的对象,请尝试在数据中创建QuestionMaster对象,而不是任何匿名类型的对象。

尝试此项,它将在末尾向现有列表添加一个新项目

列表作者=新列表
{
新作者{Name=“Mahesh Chand”,Book=“ADO.NET编程”,Price=49.95},
新作者{Name=“Neel Beniwal”,Book=“Jump Ball”,Price=19.95},
新作者{Name=“Chris Love”,Book=“Practical PWA”,Price=29.95}
};
//将更多项目添加到列表中
authors.Add(新作者{Name=“Mahesh Chand”,Book=“Graphics with GDI+”,Price=49.95});
authors.Add(新作者{Name=“Mahesh Chand”,Book=“Mastering C#”,Price=54.95});
添加(新作者{Name=“Mahesh Chand”,Book=“Jumpstart区块链”,价格=44.95})

链接


试试这个,它会在末尾向现有列表添加一个新项目

列表作者=新列表
{
新作者{Name=“Mahesh Chand”,Book=“ADO.NET编程”,Price=49.95},
新作者{Name=“Neel Beniwal”,Book=“Jump Ball”,Price=19.95},
新作者{Name=“Chris Love”,Book=“Practical PWA”,Price=29.95}
};
//将更多项目添加到列表中
authors.Add(新作者{Name=“Mahesh Chand”,Book=“Graphics with GDI+”,Price=49.95});
authors.Add(新作者{Name=“Mahesh Chand”,Book=“Mastering C#”,Price=54.95});
添加(新作者{Name=“Mahesh Chand”,Book=“Jumpstart区块链”,价格=44.95})

链接


它还将给我相同的结果。第一次添加它时,它将始终具有temp对象,因为此对象与循环之前初始化的对象具有相同的引用。temp应在循环内部初始化。它还将给我相同的结果。第一次添加它时,它将始终具有temp对象作为此对象ds在循环之前初始化与此相同的引用。应在循环内部初始化temp。