WHERE clause in LINQ to SQL MAX

entity-framework linq linq-to-entities linq-to-sql

Question

Since I'm new to Linq, I've had trouble as predicted. This is what I'm attempting to accomplish:

SELECT id, name, password
FROM users u
WHERE u.id = (SELECT MAX(u1.id) FROM users u1);

In my Linq,

var dbUsers = from u in context.Users
              where u.Id == (context.Users.Max(u1 => u1.Id))
              select u;

I always make the following exemption, though:

Unable to create a constant value of type 'Bla.Users'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

The user class is as follows:

public class Users
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
    }
}

My context class is as follows:

 public class EFDbContext : DbContext
    {
        public DbSet<User> Users{ get; set; }
    }
1
5
9/2/2011 6:27:41 PM

Popular Answer

You must choose theID property

var dbUsers = from u in context.Users
              where u.Id == (context.Users.Select(u1 => u1.Id).Max())
              select u;
8
9/2/2011 4:35:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow