选择Count by Three选项ObjectDataSource
本文关键字:选项 ObjectDataSource Three by Count 选择 | 更新日期: 2023-09-27 18:24:51
我需要GetUsers的计数,但我有3种不同的计算方法,分别是通过alpha、search和byEmail参数。在这种情况下,我能为我的ObjectDataSource SelectCount方法做些什么
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()
{
}
更新的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;
}
以下内容应该有效:
public int GetUserCount(string alpha, string search, bool byEmail, int startRowIndex, int maximumRows)
{
var users = Membership.GetAllUsers().Cast<MembershipUser>();
if (alpha != null && search == null)
{
users = users.Where(x => x.UserName.StartsWith(alpha) == true);
}
else if (search != null && !byEmail)
{
users = users.FindUsersByName(search).Cast<MembershipUser>();
}
else
{
users = users.FindUsersByEmail(search);
}
return users.Count();
}
如果FindUsersByName
和FindUsersByEmail
返回与IEnumerable<T>
相关的对象,我会对这种方法更满意,这样您就可以依赖LINQ的惰性,但在这种情况下,这并不重要,因为您正在进行计数。我建议使用某种形式的Select
来限制被查询的字段,但在后两种情况下,它们无论如何都会提取所有结果,因此在2/3的可能情况下都没有好处。如果您发现查询通常由UserName
完成,那么它可能值得添加。