I'm trying to convert a SQL statement into LINQ.
I have these models:
public class GamesNight
{
public int id { get; set; }
[DisplayName("Games Night Official Name")]
public string EventName { get; set; }
[DisplayName("Description")]
public string EventDescription { get; set; }
[DisplayName("Date and Time")]
[DataType(DataType.Date)]
public DateTime DateTime { get; set; }
public virtual ApplicationUser User { get; set; }
public bool Active { get; set; }
}
and a GamesNightAttendance, this is more or less linking a user to a games night event.
public class GamesNightAttendance
{
[Key]
public int id { get; set; }
public virtual GamesNight GameNight { get; set; }
public virtual ApplicationUser UserName { get; set; }
public bool Attendance { get; set; }
}
so a user hosts a GamesNight then other users will be able to attend the games night via the gamesnightattendancemodel.
The query I have is:
var userID = User.Identity.GetUserId();
var user = db.Users.First(x => x.Id == userID);
var result = from GNS in db.GamesNights
join GNA in db.GamesNightAttendance on GNS.id equals GNA.id
where GNS.Active & GNA.UserName == user
select new UpcomingGNAttendanceViewModel { GamesNight = GNS, Attendance = GNA.Attendance};
I get the exception:
Message = "Unable to create a constant value of type 'GNR.Models.ApplicationUser'. Only primitive types or enumeration types are supported in this context."