I have this class:
public class ExamReviewRequested
{
public List<ExamReviewRequest> Reviews { get; set;}
}
It's reviews property has a list of this class:
public class ExamReviewRequest
{
public string EventCode { get; set; }
public bool Requested { get; set; }
}
How do I search for a specific EventCode and return the selected ExamReviewRequest with that EventCode using LINQ? In my View, both these (Row.EventCode has the value I want to search for):
ExamReviewRequest er = ViewBag.ExamReviews.Single(x => x.EventCode == Row.EventCode)
ExamReviewRequest er = ViewBag.ExamReviews.Reviews.FirstOrDefault(x => x.EventCode == Row.EventCode)
return this error:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
I've looked at several posts concerning this subject but can't make any of them work.
The problem is that the type of ViewBag.ExamReviews
is dynamic
, so the compiler doesn't know what to do with your lambda expression. Just cast that part so you can use LINQ on it as normal:
var requests = (IEnumerable<ExamReviewRequest>) ViewBag.ExamReviews;
var singleRequest = requests.Single(x => x.EventCode == Row.EventCode);
See more on this question at Stackoverflow