none
EF 4.3: The result type 'System.Collections.IEnumerable' may not be abstract and must include a default constructor. RRS feed

  • Question

  • Hello,

    I have the following SQL Query:

    var a = context.Database.SqlQuery<IEnumerable>("select (select COUNT(*) from dbo.Roles ) as rolesCount, (select COUNT(*) from dbo.Users ) as usersCount");

    But I get the following error:

    base {System.SystemException} = {"The result type 'System.Collections.IEnumerable' may not be abstract and must include a default constructor."}

    Is there a way to solve this?

    Thank you,

    Miguel

    Friday, February 24, 2012 4:43 PM

Answers

  • Hi MDMoura,

    Welcome to MSDN Forum.

    It caused by you have defined the return type as IEnumerable type. Please define a DTO class which contains two properties of int type For example,

    class CountClass
        {
            public int RoleCount { get; set; }
            public int UserCount { get; set; }
        }

    user the CountClass instead of IEnumerable.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 7:06 AM
    Moderator

All replies

  • Hi MDMoura,

    Welcome to MSDN Forum.

    It caused by you have defined the return type as IEnumerable type. Please define a DTO class which contains two properties of int type For example,

    class CountClass
        {
            public int RoleCount { get; set; }
            public int UserCount { get; set; }
        }

    user the CountClass instead of IEnumerable.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 7:06 AM
    Moderator
  • Just out of curiosity,

    would it be possible to change the SQL so I could receive the values as IEnumerable?

    Thank You,

    Miguel

    Monday, February 27, 2012 1:56 PM
  • Hi MDMoura,

    IEnerable is an interface which expose the enumerator, that  supports a simple iteration over a non-generic collection. But the T-SQL only queried out two integers, why you want to them as IEnumerable type?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 28, 2012 2:44 AM
    Moderator