I need a count for the GetUsers but I have 3 different calculations either by alpha, search and byEmail parameters what can I do in this situation for my ObjectDataSource SelectCount method
public class Users
{
public IList<MembershipUser> GetUsers(string alpha, string search, bool byEmail, int startRowIndex, int maximumRows)
{
IList<MembershipUser> users = null;
if (alpha != null && search == null)
{
users = Membership.GetAllUsers().Cast<MembershipUser>().Where(x => x.UserName.StartsWith(alpha) == true).ToList();
}
else if (search != null && !byEmail)
{
users = Membership.FindUsersByName(search).Cast<MembershipUser>().Skip(maximumRows * startRowIndex).Take(maximumRows).ToList();
}
else
{
users = Membership.FindUsersByEmail(search).Cast<MembershipUser>().Skip(maximumRows * startRowIndex).Take(maximumRows).ToList();
}
return users;
}
public int GetUserCount()
{
}
Updated C#:
public IEnumerable<MembershipUser> GetUsers(string alpha, string searchBy, string searchText, int startRowIndex, int maximumRows)
{
var users = Membership.GetAllUsers().Cast<MembershipUser>();
if (alpha != null && string.IsNullOrEmpty(searchBy))
{
users = users
.Where(x => x.UserName.StartsWith(alpha, StringComparison.OrdinalIgnoreCase))
.Skip(maximumRows * startRowIndex)
.Take(maximumRows).ToList();
}
else if (searchBy == "User Name" && !string.IsNullOrEmpty(searchText))
{
users = users.Where(x => x.UserName == searchText)
.Skip(maximumRows * startRowIndex)
.Take(maximumRows).ToList();
}
else if (searchBy == "Email" && !string.IsNullOrEmpty(searchText))
{
users = users.Where(x => x.Email == searchText)
.Skip(maximumRows * startRowIndex)
.Take(maximumRows).ToList();
}
else
{
users = null;
}
return users;
}
Something like the following should work:
I would be more comfortable with this approach if
FindUsersByNameandFindUsersByEmailreturned objects related toIEnumerable<T>so you could depend on the lazy nature of LINQ, but in this case it doesn’t matter since you’re doing a count. I’d recommend some form ofSelectto limit the fields being queried over, but in your second two cases, they’ll pull all results anyway so it would be without benefit 2/3 possible situations. If you find that more often than not the query is done byUserNamethen it would probably be worth adding there.