I am new to Linq so as expected I have encountered difficulties. What I am trying to achieve is this:
SELECT id, name, password
FROM users u
WHERE u.id = (SELECT MAX(u1.id) FROM users u1);
My Linq is:
var dbUsers = from u in context.Users
where u.Id == (context.Users.Max(u1 => u1.Id))
select u;
But I always end with the following exception:
Unable to create a constant value of type 'Bla.Users'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Here is the users class:
public class Users
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
}
}
Here is my context class:
public class EFDbContext : DbContext
{
public DbSet<User> Users{ get; set; }
}
You need to select the ID
property
var dbUsers = from u in context.Users
where u.Id == (context.Users.Select(u1 => u1.Id).Max())
select u;