"序列包含多个匹配元素"调用存储过程时

时间:2014-07-31 11:18:16

标签: c# asp.net-mvc asp.net-mvc-3 stored-procedures

我正在尝试使用MyList在我的页面上显示一个列表:

  public List<ResponseType> MyList(int param1, DateTime Date)
        {
            return db.spMyStoredProcedure(param1, param2).ExecuteTypedList<ResponseType>();
        }

调用此存储过程:

    public StoredProcedure spMyStoredProcedure(int param1,DateTime Date){
        StoredProcedure sp=new StoredProcedure("spMyStoredProcedure",this.Provider);
        sp.Command.AddParameter("param1",LeadStatusID,DbType.Int32);
        sp.Command.AddParameter("Date",Date,DbType.DateTime);
        return sp;
    }

使用此课程:

public class ResponseType
    {
        public string 1 { get; set; }
        public bool2 { get; set; }
        public DateTime 3 { get; set; }
...etc.
        }

ResponseType继承另一个类,该类继承另一个类。

错误消息“Sequence包含多个匹配元素”是否意味着ResponseType(或它继承的类)有多个声明?我似乎只有一个声明,当我点击“转到声明”时,这也是Visual Studio引导我的地方。

我可以使用我传入的相同参数在SQL Server中执行存储过程,所以我不确定为什么列表无法显示。

2 个答案:

答案 0 :(得分:1)

我非常确定您在其他地方使用SingleOrDefaultSingle方法。只有当集合包含0或1个元素时,它们才会成功。为什么不尝试FirstOrDefault始终使用第一个元素(或defualt(type))?

查看SingleOrDefault的可能异常:

  1. ArgumentNullException - &gt;来源为空
  2. InvalidOperationException - &gt; 输入序列包含多个元素
  3. 另一种可能性是First()方法中的HttpPost来电。像

    这样的东西
    [HttpPost]
    public JsonResult Test(int Id)
    {
            var query = myDb.Responses
                  .Where(y => y.Id==Id).First();     
    
        return Json(new { Test= query.Name}, JsonRequestBehavior.AllowGet);
    }
    

    如果您的ID不存在则可能发生错误。

答案 1 :(得分:0)

对不起大家,我的问题是我在ResponseType中定义了一个变量,然后再次在它继承的类中。

显然你没有办法知道这一点,因为我没有包括继承的类。 不管怎样,谢谢