Reputation: 32778
I have the following code that gives me a list of id and names from the new ASP.NET MVC5 Identity:
var identityStore = new IdentityStore();
var users =
(
from user in identityStore.DbContext.Set<User>()
select new
{
id = user.Id,
name = user.UserName
}
);
How could I modify this so that it allows me to check if a UserName exists?
Here's the user class:
public class User : IUser
{
public User();
public User(string userName);
[Key]
public string Id { get; set; }
public virtual ICollection<UserLogin> Logins { get; set; }
public virtual UserManagement Management { get; set; }
public virtual ICollection<UserRole> Roles { get; set; }
public string UserName { get; set; }
}
Upvotes: 0
Views: 1089
Reputation: 13380
You can query the username with a where statement if this is what you want, e.g.
var users = from user in identityStore.DbContext.Set<User>()
where user.UserName != null && !user.UserName.Equals(string.Empty)
select ...
anyways, it is a possible duplicate of this answer LINQ syntax where string value is not null or empty
string.IsNullOrEmpty does work only in some cases, so be careful!
Or if you just look for a specific user name, simply query for it
var users = from user in identityStore.DbContext.Set<User>()
where user.UserName.Equals("whatever")
select ...
Upvotes: 1
Reputation: 6234
if(identityStore.DbContext.Set<User>().Any(u => UserName == "yourUserName"))
{
// user exists
}
else
{
// user does not exist
}
Does it fit for your requirements? Your question is bit unclear.
Upvotes: 1