Reputation: 282
I'm fairly new to LINQ and I am having a bit of trouble with my query. I am trying to fetch surveys that are associated to particular groups (SurveyGroups table is the assignment table). However, once I return my model to my View I get a null error.
This is what I have so far:
public ActionResult Index()
{
var userID = User.Identity.GetUserId();
var clientID = _db.Users.Where(u => u.Id == userID).Select(u => u.ClientID).FirstOrDefault();
var userGroups = _db.UserGroups.Where(x => x.UserID == userID).ToList();
var groupedSurveys = (from t1 in userGroups
join t2 in _db.SurveyGroups
on t1.GroupID equals t2.GroupID
select new { t2.SurveyID }).ToList();
var surveys = (from t11 in groupedSurveys
join t22 in _db.Surveys.Where(s => s.ClientID == clientID)
on t11.SurveyID equals t22.SurveyID
select new { t22.ClientID, t22.Name}).ToList();
return View("~/Views/User/Dashboard.cshtml", surveys);
}
Any guidance would be greatly appreciated!
UPDATED:
The error I am receving is the following:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType6
5[System.Int32,System.Nullable1[System.Int32],System.Nullable
1[System.Int32],System.Nullable1[System.Int32],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[SM_XPRESS.Models.Survey]'.
Upvotes: 1
Views: 90
Reputation: 52290
In this line:
select new { t22.ClientID, t22.Name}
...you are creating a new instance of an anonymous type, based on the data in t22
(which is a survey record).
However, the list you need to provide to your view needs to be a list of Survey instances, not a list of anonymous type instances.
So change it to
select t22
and you're good to go.
Upvotes: 2