Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a project where I query users by first letter:

repository.GetAll().Where(q => q.BrukerIdent.StartsWith(letter.ToString())).ToList();

..where repository.GetAll() returns an IQueryable<Bruker>, BrukerIdent is a string that contains the username, and letter is a char-value coming in. This works perfectly, except that I also want to get users that starts with digits. And I don't want to sort by separate digits.

My mind yells for a StartsWith("d") but as far as I have found out it doesn't work this way. I have also thought of doing a 10-way OR clause, but that would look like spaghetti, and I'm not sure of the efficiency.

Is there any "right" way to do it like this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

If this is for LINQ-to-SQL you could use SqlMethods.Like method here:

var result = repository
    .GetAll()
    .Where(q => SqlMethods.Like(q.BrukerIdent, "[0-9]%"))
    .ToList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...