SQL Syntax C# Filtering

Take the following list:

var persons = new Person[]
{
    new Person { Name = "Adrian", Age=34 },
    new Person { Name = "Lilach", Age=32 },
    new Person { Name = "Yuval", Age=5 },
    new Person { Name = "Alon", Age=3 },
};

Of the following class:

[DebuggerDisplay("{Name} ({Age})")]
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Using LINQ, we can easily select some items using lambda expressions, for example:

var persons = new Person[]
{
    new Person { Name = "Adrian", Age=34 },
    new Person { Name = "Lilach", Age=32 },
    new Person { Name = "Yuval", Age=5 },
    new Person { Name = "Alon", Age=3 },
};

var selected = persons.Where(p => p.Age > 5 || p.Name.Contains("va"));

And get this result:

image

The Where method accepts a hard-coded expression and if we want to provide the user the ability to define filters, we have to build the expression at runtime.

In-addition to building the expression at runtime, we have to give the user a proper way to input such an expression.

A simple approach to giving the user the ability to filter the data may be using an SQL syntax, for example:

var persons = new Person[]
{
    new Person { Name = "Adrian", Age=34 },
    new Person { Name = "Lilach", Age=32 },
    new Person { Name = "Yuval", Age=5 },
    new Person { Name = "Alon", Age=3 },
};

var selected = persons.Where("Age > 5 OR Name LIKE '*va*'");

Continue reading

Posted in .NET | Tagged | 3 Comments

CLAP v3

CLAP is a magical .NET Command-Line Automatic Parser. It has plenty of features and is extremely easy to use. If you are new to CLAP – please see the short intro here.

CLAP has a new version and it brings some more cool features to the tons of already-existing ones.

New in this version, since v2:

  • Expression Validation
  • Support for Custom Types
  • File-Input
  • Interception
  • Multi-Parsers

Everything is fully documented in CLAP’s official web site:

http://adrianaisemberg.github.com/CLAP

Continue reading

Posted in .NET, CLAP, Command-Line | Tagged , | Leave a comment

CLAP has a new home with full documentation

Here:

http://adrianaisemberg.github.com/CLAP

Posted in CLAP, Command-Line | Tagged , | Leave a comment