Foreach语句不能对'?'类型的变量进行操作因为'?'不包含'GetEnumerator'

时间:2016-11-24 05:11:45

标签: c# entity-framework

我对我正在使用的数据库的新副本进行了一些更新,并从数据库生成了edmx文件,我修复了几个存储过程,这些过程给我带来了问题,但是在这个方法中我一直收到此错误< / p>

  

错误CS1579 foreach语句无法对类型'?'的变量进行操作因为'?'不包含'GetEnumerator'

的公共定义

我认为它与此错误相关

  

错误CS1936无法找到源类型“int”的查询模式的实现。 '选择'未找到

我已经四处寻找解决方案,并没有找到任何可靠的解决方案。

我的DAL中抛出错误的方法是

public List<NewCustomer> GetCustomerToEditByID(int id)
    {
        ExoEntities = new ExoEntities();
        List<NewCustomer> lst = new List<NewCustomer>();

        var query = from a in  ExoEntities.usp_GetCustomerByID(id)
                    select a;

        foreach (var b in query)
        {
            lst.Add(new NewCustomer
            {
                CustomerID = b.CustomerID,
                FirstName = b.FirstName,
                LastName = b.LastName,
                YearBuilt = b.YearBuilt,
                Line1 = b.Line1,
                Line2 = b.Line2,
                City = b.City,
                ZipCode = b.ZipCode,
                StateID = (int)b.StateID,
                StateName = b.StateName,
                County = b.County,
                SubDivisionID = (int)b.SubDivisionID,
                ContactName = b.ContactName,
                Phone = b.Phone,
                OtherPhone = b.OtherPhone,
                Cell = b.Cell,
                Fax = b.Fax,
                Email = b.Email
            });
        }

        return lst;
    }

数据类是

public class NewCustomer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string DateCreated { get; set; }
        public int CreatedBy { get; set; }
        public string YearBuilt { get; set; }
        public byte IsActive { get; set; }
        public int CustomerTypeID { get; set; }

        // Address
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string Line3 { get; set; }
        public string City { get; set; }
        public string ZipCode { get; set; }
        public int StateID { get; set; }
        public string StateName { get; set; }
        public string County { get; set; }
        public int SubDivisionID { get; set; }

        // Contact
        public string ContactName { get; set; }
        public string Phone { get; set; }
        public string OtherPhone { get; set; }
        public string Cell { get; set; }
        public string Pager { get; set; }
        public string Fax { get; set; }
        public string Email { get; set; }
        public byte ContactIsActive { get; set; }
    }

这是存储过程的EF上下文类

public virtual int usp_GetCustomerByID(Nullable<int> customerID)
    {
        var customerIDParameter = customerID.HasValue ?
            new ObjectParameter("CustomerID", customerID) :
            new ObjectParameter("CustomerID", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetCustomerByID", customerIDParameter);
    }

我的存储过程是

    Create procedure [dbo].[usp_GetCustomerByID]
(
    @CustomerID int
)
AS
SET NOCOUNT OFF
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @ERROR_SEVERITY int,
        @MESSAGE varchar(1000),
        @ERROR_NUMBER int,
        @ERROR_PROCEDURE nvarchar(200),
        @ERROR_LINE int,
        @ERROR_MESSAGE nvarchar(4000);

begin try
    select 
          caxref.CustomerID,
          caxref.AddressID,
          customer.FirstName,
          customer.LastName,
          customer.YearBuilt,
          address.Line1,
          address.Line2,
          address.City,
          address.ZipCode,
          address.StateID,
          address.County,
          state.StateName,
          address.SubDivisionID,
          contact.ContactName,
          contact.Phone,
          contact.OtherPhone,
          contact.Cell,
          contact.Fax,
          contact.Email
    from [CustomerAddressXREF] caxref
        left join [Customer] customer on customer.CustomerID = caxref.CustomerID
        left join [Address] address on address.AddressID = caxref.AddressID
        left join [SubDivision] subdivision on subdivision.SubDivisionID = address.SubDivisionID
        left join [CustomerContactXREF] ccxref on ccxref.CustomerID = customer.CustomerID
        left join [Contact] contact on contact.ContactID = ccxref.ContactID
        inner join [State] state on state.StateID = address.StateID
    where customer.CustomerID = @CustomerID

end try
BEGIN CATCH
    SET @ERROR_SEVERITY = ISNULL(ERROR_SEVERITY(),'');
    SET @ERROR_NUMBER = ISNULL(ERROR_NUMBER(),'');
    SET @ERROR_PROCEDURE = ISNULL(ERROR_PROCEDURE(),''); 
    SET @ERROR_LINE = ISNULL(ERROR_LINE(),'');
    SET @ERROR_MESSAGE = ISNULL(ERROR_MESSAGE(),'');

    -- Test if the transaction is uncommittable.
    IF (XACT_STATE()) = -1
        BEGIN
            --PRINT N'The transaction is in an uncommittable state. Rolling back transaction.'
            ROLLBACK TRANSACTION;
        END;

    -- Test if the transaction is active and valid.
    IF (XACT_STATE()) = 1
        BEGIN
            --PRINT N'The transaction is committable. Committing transaction.'
            COMMIT TRANSACTION;   
        END;

    SET @MESSAGE = 'Error Occured in Stored Procedure ' + cast(@ERROR_PROCEDURE as varchar(200)) + 
                    '; Line Number ' + cast(@ERROR_LINE as varchar) + 
                    '; Message: [' + cast(@ERROR_NUMBER as varchar) + '] - '
                    + cast(@ERROR_MESSAGE as varchar(255))

    RAISERROR(@MESSAGE, @ERROR_SEVERITY, 1);
END CATCH;

我在SSMS中测试了存储过程,它返回了我需要的所有数据

所以我不确定问题在哪里。

我已经删除了我的EDMX文件几次,并从数据库中重新生成了它,但这不起作用,我仍然得到这两个错误。对于一个解决方案来说,它可能显而易见,但它并没有突然出现在我身上

1 个答案:

答案 0 :(得分:1)

您的usp_GetCustomerByID C#函数返回单个int值,而不是对象集合,因此您无法将其用作查询源。 from a in ExoEntities.usp_GetCustomerByID(id)无效。

如果要查询,usp_GetCustomerByID的返回类型应为IEnumerable<sometype>。您可能我可以使用IEnumerable<NewCustomer>作为返回类型,但不清楚这是否是您的实体类型。然后,您不必迭代它并创建新的NewCustomer对象。你可以拨打return usp_GetCustomerByID(id).ToList()

此外,查询from a in <source> select a本质上是返回<source>上的迭代器,因此您可以使用:

foreach (var b in <source>)