Reference > Operators > Logical

Like

  • 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022

Use the Like operation to find values that match the provided pattern.

core concept: logical operators

Syntax

{string_element}.Like({pattern})

Arguments

string_element
The element to search for `pattern`.
pattern
The pattern to use to find values that match.

Returns

FilterExpression
An expression that can be used anywhere a logical expression can be used:
  • Where clause
  • Join clause
  • Having clause

Prefix the Like expression with ! to negate the expression.

Examples

Where Clause

Find all persons where their first name starts with the letter 'J'

IEnumerable<Person> persons = db.SelectMany<Person>()
    .From(dbo.Person)
    .Where(dbo.Person.FirstName.Like("J%"))
    .Execute();

Find all persons where their first name does not start with the letter 'J'

IEnumerable<Person> persons = db.SelectMany<Person>()
    .From(dbo.Person)
    .Where(!dbo.Person.FirstName.Like("J%"))
    .Execute();

Having Clause

Find unique product names that have the word 'Book' in the name

IEnumerable<string> product_counts = db.SelectMany(
        dbo.Product.Name
    )
    .From(dbo.Product)
    .GroupBy(dbo.Product.Name)
    .Having(dbo.Product.Name.Like("%Book%"))
    .Execute();

Join Clause

Find all persons who live in a zip code that starts with '80'

IEnumerable<Person> persons = db.SelectMany<Person>()
    .From(dbo.Person)
    .InnerJoin(dbo.PersonAddress).On(dbo.Person.Id == dbo.PersonAddress.PersonId)
    .InnerJoin(dbo.Address).On(
        dbo.Address.Zip.Like("80%")
        &
        dbo.PersonAddress.AddressId == dbo.Address.Id
    )
    .Execute();

ind all persons who don't live in a zip code that starts with '80'

IEnumerable<Person> persons = db.SelectMany<Person>()
    .From(dbo.Person)
    .InnerJoin(dbo.PersonAddress).On(dbo.Person.Id == dbo.PersonAddress.PersonId)
    .InnerJoin(dbo.Address).On(
        !dbo.Address.Zip.Like("80%")
        &
        dbo.PersonAddress.AddressId == dbo.Address.Id
    )
    .Execute();
Previous
In

© 2024 dbExpression. All rights reserved.