LINQ to SQL 查询以确定值是否以数字开头

LINQ to SQL query to determine if value starts with numeric(LINQ to SQL 查询以确定值是否以数字开头)
本文介绍了LINQ to SQL 查询以确定值是否以数字开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个项目,我通过首字母查询用户:

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

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

..where repository.GetAll() 返回一个 IQueryableBrukerIdent 是一个包含用户名的字符串,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 查询以确定值是否以数字开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)