Search code examples
c#asp.net-mvcrepository-patternormlite-servicestack

Invalid Argument in Method when pulling from repository


I am trying to learn how this Repository works by disecting it. I am really lost so was hoping the community would help me out a bit. Please keep in mind I am new to MVC so don't tear me apart to much. The error I am getting is because I have invalid arguments. I think it is from my 3rd argument but I am not quite sure how to fix it.

ResponentRepository.GetRespondents() is the problem Notice below.

In my controller I have:

        List<int> projectIdsToInclude = new List<int>();
        projectIdsToInclude.Add(4);

        List<int> statusesToInclude = new List<int>();
        statusesToInclude.Add(1);

        List<string> fieldsToInclude = new List<string>();
        fieldsToInclude.Add("firstName");



        IEnumerable<int> pID = projectIdsToInclude;
        IEnumerable<string> fields = fieldsToInclude;

        var respondents = RespondentRepository.GetRespondents(UserSession, pID, statusesToInclude, fields);

In the Respository it has:

public List<Respondent> GetRespondents(UserSessionData sessionData, IEnumerable<int> projectIdsToInclude, IEnumerable<RespondentStatus> statusesToInclude, IEnumerable<string> fieldsToInclude)
        {
            var request = new RespondentRequest
            {
                Options = new RequestOptions
                {
                    Include = fieldsToInclude,
                    //OrderBy = new List<OrderBy> { new OrderBy { Direction = OrderByDirection.Descending, Field = Create.Name<Respondent>(r => r.LastActionDate) } },
                    Filter = new LogicExpression
                    {
                        ExpressionA = new InExpression<int>
                        {
                            Field = Create.Name<Respondent>(r => r.ProjectId),
                            Values = projectIdsToInclude.ToList()
                        },
                        ExpressionB = new InExpression<RespondentStatus>
                        {
                            Field = Create.Name<Respondent>(r => r.RecruitingStatus),
                            Values = statusesToInclude.ToList()
                        },
                        Operator = SqlOperator.And
                    },

                }
            };

            return Get(request, sessionData).Data;
        }

My Invalid Argument I think is coming from the argument that is IEnumerable. So when I follow RespondentStatus I see this:

public enum RespondentStatus : int
    {
        [Description("Not Contacted")]
        NotContacted = 0,
        [Description("Pre-Screened")]
        PreScreened = 1,
        [Description("On Hold")]
        Hold = 2,
        [Description("Initial Refusal")]
        InitialRefusal = 3,
        [Description("Qualified Refusal")]
        QualifiedRefusal = 4,
        [Description("Remove From Panel")]
        RemoveFromPanel = 5,
        Terminated = 6,
        Complete = 7,
        //Probably should never reuse '8' as a status since retiring "Approved" status
        Approved = 8,

        // Phone contact statuses
        [Description("No Answer")]
        NoAnswer = 30,
        Busy = 31,
        [Description("Left Message")]
        LeftMessage = 32,
        [Description("Call Back Later")]
        CallBackLater = 33,
        [Description("Non-working Number")]
        NonWorkingNumber = 34,
    }

How do I fix my code in the controller to pull respondents from this repository? I just want to return some respondents. I think if I can do that I can further dissect from there. Can someone explain what I should do to make this work or what I am missing?


Solution

  • statusesToInclude should be IEnumerable<RespondentStatus> You are expecting IEnumerable<RespondentStatus> in you method, but calling it with List<int>.

    Either change your method signature or your calling code so that the two statusesToInclude variables match up and have the same type.

    For example:

    List<RespondentStatus> statusesToInclude = new List<RespondentStatus>();
            statusesToInclude.Add((RespondentStatus)1);