1

I have this code snippet that queries a SQL table that contains first, middle and last names. The model requires a first name and last name but, middle name is optional. Here's the issue with this code that I'm having. No matter what value is entered for a middle name (blank or value present) records that do not contain a middle name are not returned in the result set. Searching for John Smith will return "John M Smith" but not "John Smith". I should also note that the middle name column in the database can contain null, blank or string values. It's an old MS SQL database with a terribly written VB front end.

Code:

var query = (from i in ctx.Individuals
     join l in ctx.Licenses on i.IndividualId equals l.IndividualId
     where i.FirstName.StartsWith(firstName) && i.LastName.StartsWith(lastName) && i.MiddleName.StartsWith(middleName ?? i.MiddleName)
     select new IndividualDTO()
     {
         IndividualId = i.IndividualId,
         FirstName = i.FirstName,
         LastName = i.LastName,
         MiddleName = i.MiddleName,
         LicenseNumber = l.LicenseNumber
     }).OrderBy(i => i.FirstName).OrderBy(i => i.LastName).Take(50).ToList();
1
  • Some databases treat empty strings as null. But we probably need to see the query that gets generated. Aug 3, 2015 at 22:11

2 Answers 2

2

We wanted to match if the following is happening MiddleName starts with middleName or middleName is empty or null This means we want to have our boolean expression to succeed if

String.IsNullOrWhitespace(i.MiddleName) OR i.MiddleName.StartsWith(middleName).

  • In case of middleName empty or null than this will evaluate && (true || Doesn't matter) which will give us a match.

  • And In case of i.MiddleName starts with middleName than this will evaluate && (false || true) which also give us a match.

You should change

i.MiddleName.StartsWith(middleName ?? i.MiddleName)

To

(String.IsNullOrWhitespace(i.MiddleName) || i.MiddleName.StartsWith(middleName))
3
  • It would probably help if you explained why this might resolve the issue.
    – Dave Zych
    Aug 3, 2015 at 22:18
  • This is including the blank or nulls now but if the optional middle name field is left blank then those that have a middle name are now excluded. Ideas? Aug 3, 2015 at 22:20
  • @DaveZych: Explanasion added.
    – Orel Eraki
    Aug 3, 2015 at 23:06
1

I would actually redo this to only include your conditions when necessary. This way you avoid the null or empty middle name. If the search strings could be whitespace use string.IsNullOrWhitespace instead. Also you'll want to use ThenBy in your ordering to keep your initial order.

var query = from i in ctx.Individuals
    join l in ctx.Licenses on i.IndividualId equals l.IndividualId
    select new IndividualDTO
    {
        IndividualId = i.IndividualId,
        FirstName = i.FirstName,
        LastName = i.LastName,
        MiddleName = i.MiddleName,
        LicenseNumber = l.LicenseNumber
    };

if (!string.IsNullOrEmpty(firstName))
{
    query = query.Where(i => i.FirstName.StartsWith(firstName));
}
if (!string.IsNullOrEmpty(lastName))
{
    query = query.Where(i => i.LastName.StartsWith(lastName));
}
if (!string.IsNullOrEmpty(middleName))
{
    query = query.Where(i => i.MiddleName.StartsWith(middleName));
}

var result = query
    .OrderBy(i => i.FirstName)
    .ThenBy(i => i.LastName)
    .Take(50)
    .ToList();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.