I have a Web API 2 project which is backed by a SQL DB. I have a table, UserAlerts, with fields Lat, Lon, Timestamp, UserID.
I have a UserAlert controller, and UserAlert model with fields matching value and type of the fields in the Db. What is the correct way to query the database and return a JSON object containing the rows which contain UserID=UserID from client?
I have tried
List<UserAlerts> userAlerts = await db.UserAlerts.Where(x => x.UserID == UserID)
.Select(x => new UserAlerts
{
Lat = x.Lat,
Lon = x.Lon,
TimeTriggered = x.TimeTriggered,
TimeEnded = x.TimeEnded,
UserID = x.UserID
}).ToListAsync();
But of course I get "The entity or complex type ',namespace>.Models.UserAlerts' cannot be constructed in a LINQ to Entities query.".
Any advice would be appreciated, I hope the question is clear!