简体   繁体   English

LINQ - 在最低和最高字符串之间查找

[英]LINQ - find between lowest and highest string

I would like to find string numbers between "0000" and "9999" inside larger strings from "qwt0000abc" to "qwt9999abc".我想在从“qwt0000abc”到“qwt9999abc”的较大字符串中找到“0000”和“9999”之间的字符串编号。

MSSQL where id between "qwt0000abc" and "qwt9999abc" MSSQL,其中 id 在“qwt0000abc”和“qwt9999abc”之间

How I would write the same LINQ query?我将如何编写相同的 LINQ 查询?

var selectedOrders = orders.Where(f => f.Id between "qwt0000abc" and "qwt9999abc");

Try this:尝试这个:

var selectedOrders = arr.Where(x => Regex.IsMatch(x, ".*[0-9]{4}.*"));

or this, if you need exactly qwt prefix and abc suffix.或者这个,如果你需要qwt前缀和abc后缀。

var selectedOrders = arr.Where(x => Regex.IsMatch(x, "qwt[0-9]{4}abc"));

.* here is any literal any number of times(include zero), [0-9]{4} is digit between 0 and 9, exactly 4 digits. .*这里是任意次数的任何文字(包括零), [0-9]{4}是 0 到 9 之间的数字,正好是 4 位。

Assuming you only want rows with that same prefix and suffix, then just replace between like this:假设您只想要具有相同前缀和后缀between行,那么只需像这样替换:

var selectedOrders = orders.Where(f => f.Id.CompareTo("qwt0000abc") >= 0 && f.Id.CompareTo("qwt9999abc") < 0).ToList() ;

And if this is EF you can perhaps use Like:如果这是 EF,您也许可以使用 Like:

var selectedOrders = orders.Where(f => EF.Functions.Like(f.Id, "qwt[0-9][0-9][0-9]abc")).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM