0

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!

2 Answers 2

1

If the types are the same (they're both called UserAlert after all...), then do you need the .Select() clause at all? Try:

List<UserAlerts> userAlerts = await db.UserAlerts
                                      .Where(x => x.UserID == UserID)
                                      .ToListAsync();

If the types are in fact different, then you can materialize the collection first before transforming it:

List<UserAlerts> userAlerts = await db.UserAlerts
                                      .Where(x => x.UserID == UserID)
                                      .ToListAsync()
                                      .Select(x => new UserAlerts
                                          {
                                              Lat = x.Lat,
                                              Lon = x.Lon,
                                              TimeTriggered = x.TimeTriggered,
                                              TimeEnded = x.TimeEnded,
                                              UserID = x.UserID
                                          })
                                      .ToListAsync();;

This would incur a performance hit if what you're materializing is considerably more data than what you're transforming it into, so be careful.

Sign up to request clarification or add additional context in comments.

1 Comment

Bang on the money, optino one works like a charm, thank you! My old backend was php with mysql, these are murky waters for me ;)
0

With

"The entity or complex type ',namespace>.Models.UserAlerts' cannot be constructed in a LINQ to Entities query."

This is because there is a table/class named 'UserAlerts' in your context. You should select anonymously like following

  List<UserAlerts> userAlerts = await db.UserAlerts.Where(x => x.UserID == UserID)
        .Select(x => new 
        {
            Lat = x.Lat,
            Lon = x.Lon,
            TimeTriggered = x.TimeTriggered,
            TimeEnded = x.TimeEnded,
            UserID = x.UserID
        }).ToListAsync();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.