Narazana
Narazana

Reputation: 1950

Using LINQ to SQL to query a word started with something within string

I'd take both C# or VB.NET suggestion.

I'm using LINQ to SQL to query some data.

Here's my Employees table colummns:

-FirstName (or givename i.e. John) -LastName (has middle name and family name seperated by space i.e. Mac Tenny

Some people have middle name and some don't. Here's some sample data.

-FirstName LastName

I want to query for people who have their middle or last name started with search criteria. Here's how I search for employees with middle name started with "joe" :

            query = From emp In db.Employees _
                    Where emp.LastName.StartsWith("joe") _
                    Select emp

How do I query for employees with family name(last part of the LastName) started with "joe"?

Thank you.

Upvotes: 0

Views: 1051

Answers (2)

LukeH
LukeH

Reputation: 269358

' return employees where family name starts with "joe"
query = From emp In db.Employees _
        Where emp.LastName.Substring(emp.LastName.LastIndexOf(" ") + 1) _
                          .StartsWith("joe") _
        Select emp

' return employees where middle name or family name starts with "joe"
query = From emp In db.Employees _
        Where emp.LastName.StartsWith("joe") _
            OrElse emp.LastName.Contains(" joe") _
        Select emp

Upvotes: 2

Matt
Matt

Reputation: 1038

query = From emp In db.Employees _
   Where emp.LastName.StartsWith("joe") _
   or Lastname.contains(" joe") _
   Select emp

Upvotes: 3

Related Questions