繁体   English   中英

如何使用Linq2Sql搜索一系列字符?

[英]How can I search for a range of characters using Linq2Sql?

我正在尝试合并此SQL查询的LINQ等效项。 该数据库是SQL Server。

SELECT * FROM Party
WHERE Partyname LIKE '[a-m]%z'

此查询将返回所有记录,其中partyname列可以以-m之间的任何字母开头,并且必须以字母z结尾。

如果我想做一个LINQ等效产品,该怎么做? 我尝试以这种方式执行此操作,但是显然有更好的方法。

Parties.Where(p => (p.Partyname.StartsWith("a") || p.Partyname.StartsWith("b") ||
p.Partyname.StartsWith("c")) && p.Partyname.EndsWith("Z"))
.Select(x => new { x.Party_id, x.Partyname, x.Party_no, x.Reference, x.Input_dt })

注意:Linqpad使表的名称复数。

也许您可以在linq和常规之间使用合并

System.Text.RegularExpressions.Regex searchTerm =  
        new System.Text.RegularExpressions.Regex(@"Visual (Basic|C#|C\+\+|Studio)");  

var queryMatchingFiles =  
        from file in fileList  
        where file.Extension == ".htm"  
        let fileText = System.IO.File.ReadAllText(file.FullName)  
        let matches = searchTerm.Matches(fileText)  
        where matches.Count > 0  
        select new  
        {  
            name = file.FullName,  
            matchedValues = from System.Text.RegularExpressions.Match match in matches  
                            select match.Value  
        };  

https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/linq/how-to-combine-linq-queries-with-regular-expressions

暂无
暂无

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

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