user19072736
user19072736

Reputation:

System.InvalidOperationException: 'Sequence contains no matching element'

May I know why that error keeps pointing to "String isEmailVerified ...."?

public JsonResult GetMemberCounts([FromBody] ChartFilterRequest filter)
        {
            DateTime startDate = DateTime.Parse(filter.MainFilter.First(m => m.Name == "startDate").Value as string);
            DateTime endDate = DateTime.Parse(filter.MainFilter.First(m => m.Name == "endDate").Value as string);
            String isEmailVerified = filter.MainFilter.First(m => m.Name == "isEmailVerified").Value as string;


            var data = _dashboardComponent.GetMemberCount(startDate, endDate, isEmailVerified);

            return new JsonResult(data);
        }

Upvotes: 1

Views: 6013

Answers (1)

Xinran Shen
Xinran Shen

Reputation: 9993

Try using FirstOrDefault and LastOrDefault instead of First and Last, these methods will return the default value of the type they are invoked for if no elements match the lambda expression you provide as a parameter.

In your project, You just use filter.MainFilter.First(xxxx) to select the data, So if no elements match the lambda expression you provide as a parameter,First() will throw an exception, So here will report this error.

Upvotes: 1

Related Questions