問題描述
我有一個項目,我通過首字母查詢用戶:
I have a project where I query users by first letter:
repository.GetAll().Where(q => q.BrukerIdent.StartsWith(letter.ToString())).ToList();
..where repository.GetAll()
返回一個 IQueryable
,BrukerIdent
是一個包含用戶名的字符串,letter
是一個傳入的字符值.這很好用,除了我還想獲得以數字開頭的用戶.而且我不想按單獨的數字排序.
..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.
我的腦海里呼喊著 StartsWith("d")
但據我所知,它不是這樣工作的.我也想過做一個 10 路 OR 子句,但這看起來像意大利面條,我不確定效率.
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?
推薦答案
repository.GetAll().Where(q => Char.IsNumber(q.BrukerIdent[0]))
MSDN
var numbers = Enumerable
.Range(0, 10)
.Select(i => i.ToString(CultureInfo.InvariantCulture));
// var numbers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
// var numbers = HashSet<int> { ... };
var q = from b in repository.GetAll()
where numbers.Contains(b.BrukerIdent.FirstOrDefault())) //[0]
select b;
這篇關于LINQ to SQL 查詢以確定值是否以數字開頭的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!