0

I am trying to incorporate the LINQ equivalent of this SQL query. The database is SQL Server.

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

This query will return all the records where the partyname column can start with any letter between a - m and must end with the letter z.

If I wanted to do a LINQ equivalent, how can I do this? I tried to do this in this fashion, but clearly there is a better way.

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 })

Note: Linqpad pluralizes the name of the table.

abhi
  • 3,082
  • 6
  • 47
  • 73

1 Answers1

0

Maybe you can use combine between linq and regular

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://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-combine-linq-queries-with-regular-expressions

novac
  • 116
  • 8