Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net EF6无法调用存储过程并返回复杂类型_Asp.net_Linq_Entity Framework - Fatal编程技术网

Asp.net EF6无法调用存储过程并返回复杂类型

Asp.net EF6无法调用存储过程并返回复杂类型,asp.net,linq,entity-framework,Asp.net,Linq,Entity Framework,我正在为我的每个实体创建具有所有数据库访问功能的类(GetById,Insert,Update,Delete,GetList) 对于我的GetList函数,我总是使用存储过程,因为它们可能变得非常复杂(我更喜欢TSQL而不是Linq)。我无法返回正确的类型。我希望一个对象列表能够在我的代码中使用,以填充网格等 有人能检查一下我的代码并告诉我哪里出错了吗 public Customers_GetList_Result GetList(string CustomerName) { using

我正在为我的每个实体创建具有所有数据库访问功能的类(
GetById
Insert
Update
Delete
GetList

对于我的
GetList
函数,我总是使用存储过程,因为它们可能变得非常复杂(我更喜欢TSQL而不是Linq)。我无法返回正确的类型。我希望一个对象列表能够在我的代码中使用,以填充网格等

有人能检查一下我的代码并告诉我哪里出错了吗

public Customers_GetList_Result GetList(string CustomerName) {
    using (var db = new DataEntities()) {
        return db.Customers_GetList(CustomerName);
    }
}
我得到一个错误:

无法将类型“System.Data.Entity.Core.Objects.ObjectResult”隐式转换为“DAL.Customers\u GetList\u Result”


生成的存储过程返回

ObjectResult
是一个集合。如果希望返回类型为T的单个元素,则需要执行以下操作

public Customers_GetList_Result GetList(string CustomerName) {
    using (var db = new DataEntities()) {
        return db.Customers_GetList(CustomerName).Single();
    }
}

嗯,我想我知道我想要什么了:

public IEnumerable<Customers_GetList_Result> GetList(string CustomerName) {
   using (var db = new DataEntities()) {
       return db.Customers_GetList(CustomerName);
   }
}
public IEnumerable GetList(字符串CustomerName){
使用(var db=new DataEntities()){
返回db.Customers\u GetList(CustomerName);
}
}

我忘了在函数声明中添加
IEnumerable
。很抱歉从VB.net转到C#。

调用映射到存储过程的函数时,我遇到了相同的错误,我想返回一个复杂的数据类型。我只在选择一张唱片时得到它,上面的建议很有帮助。原代码为:

        using (ede = new EDEntities())
        {
            var item = ede.SelectMatrixItem(itemCode);
            return item;
        }
当我把它改成:

using (ede = new EDEntities())
            {

                var item = ede.SelectMatrixItem(itemCode).FirstOrDefault();
                return item;
            }
然后错误消失了。我的问题是FirstOrDefault还是SingleOrDefault是更好的选择。ItemCode是主键


Joey

@user2272527,显示您的
客户\u GetList\u结果
类,并在问题中更新它