LINQ to SQL的查询,以确定是否值以数字数字、LINQ、to、SQL

2023-09-02 12:00:20 作者:好多狗 好多你 i

我有我的查询用户可以通过首字母项目:

  repository.GetAll()式(Q => q.BrukerIdent.StartsWith(letter.ToString()))。了ToList()。
 

..其中 repository.GetAll()返回的IQueryable<布鲁克> BrukerIdent 是包含用户名,而字母是一个char值进来的字符串。这完美的作品,但我也希望得到用户与数字开头。而且我不希望由不同的数字进行排序。

我的心中骂了 StartsWith( D)但据我发现它不以这种方式工作。我也想过做一个10路或条款,但看​​起来像面条,而我不知道效率。

有没有正确的方式做到这一点是这样?

解决方案

  repository.GetAll()式(Q => Char.IsNumber(q.BrukerIdent [0]))。
 

MSDN

  VAR数=可枚举
                     .Range(0,10)
                     。选择(ⅰ=> i.ToString(CultureInfo.InvariantCulture));

//变种号码=新[] {0,1,2,3,4,5,6,7,8,9);
//变种数=的HashSet< INT> {...};

变种Q =从B在repository.GetAll()
        其中numbers.Contains(b.BrukerIdent.FirstOrDefault()))// [0]
        选择B:
 
在vs2010中使用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() 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?

解决方案

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;