Skip to content

Instantly share code, notes, and snippets.

@kiriappeee
Created November 26, 2011 16:02
Show Gist options
  • Save kiriappeee/1395903 to your computer and use it in GitHub Desktop.
Save kiriappeee/1395903 to your computer and use it in GitHub Desktop.
Emulating 'LIKE' operator using Linq with arrays
/**
* If you search the internet I haven't seen much of a solution for people being able to emulate
* the LIKE operator in C# using LINQ with arrays. Most of the solutions run along the lines of working
* with the contains() method.
* But the LIKE operator is essentially somewhat of a dumbed down version of regex.
* In this example I have several class names and I wanted to know which classes match a particular
* pattern; the pattern I wanted to match was all classes ending with 'det'
* And given below is the working example of this as a console application.
**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace TestBed
{
class Program
{
static void Main(string[] args)
{
String[] classnames = { "fjouhed", "fjoudet", "fgreched", "fgrecdet", "fdrecdet", "detfhrec" };
var classname =
from cn in classnames
where Regex.IsMatch(cn, "det$")
select cn;
foreach (var cn in classname)
{
Console.WriteLine(cn);
}
while (true) { }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment