1

Could somone tell me why this statement return nothing and how to correct it. What im trying todo is select User where username starts with the letter from the alphabet

alpha = "A" "B" "C" "D" etc. both don't work

IList<MembershipUser> users = Membership.GetAllUsers().Cast<MembershipUser>()
.Where(x => x.UserName.StartsWith(alpha) == true).ToList();

IList<MembershipUser> users = Membership.GetAllUsers().Cast<MembershipUser>()
.Where(x => x.UserName.StartsWith(alpha)).ToList();
ONYX
  • 5,679
  • 15
  • 83
  • 146
  • possible duplicate of [Like Operator in Entity Framework?](http://stackoverflow.com/questions/1033007/like-operator-in-entity-framework). I'd imagine it should work similar in your environment. – M.Babcock Feb 16 '12 at 05:12
  • 1
    what does `Membership.GetAllUsers()` return? – mauris Feb 16 '12 at 05:12
  • @thephpdeveloper - [Membership.GetAllUsers](http://msdn.microsoft.com/en-us/library/dy8swhya.aspx) returns a [System.Web.Security.MembershipUserCollection](http://msdn.microsoft.com/en-us/library/system.web.security.membershipusercollection.aspx). – M.Babcock Feb 16 '12 at 05:14
  • Get All users return a collection of MembershipUser – ONYX Feb 16 '12 at 05:15
  • Then why do we need a `Cast()` here? – mauris Feb 16 '12 at 05:16
  • 1
    First, I'm assuming `Membership.GetAllUsers().Cast()` isn't null. Second, `StartsWith` is case sensitive - have you tried using `x.UserName.ToLower().StartsWith(alpha.ToLower())`? – Simon Hartcher Feb 16 '12 at 05:16

2 Answers2

3

StartsWith is case sensitive

Use x.UserName.ToLower().StartsWith(alpha.ToLower())

Simon Hartcher
  • 3,400
  • 3
  • 30
  • 34
3

It's better to use this

IList<MembershipUser> users = Membership.GetAllUsers().Cast<MembershipUser>()
.Where(x => x.UserName.StartsWith(alpha, StringComparison.OrdinalIgnoreCase)).ToList();
Lonli-Lokli
  • 3,357
  • 1
  • 24
  • 40
  • This is actually better than simons so i'll mark this as answered because it is better – ONYX Feb 16 '12 at 22:38