KyloRen
KyloRen

Reputation: 2741

LINQ query with multiple StartsWith clause?

I have a LINQ query that works as it is supposed to as follows,

var query = DataContext.TenantDataServerTables.Where(p => 
             p.Nursing_Home_Section == homeSection &&
             p.Tenant_Kana_Last.ToString().StartsWith(@"ア") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"イ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"ウ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"エ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"オ"));
        }

Is there a way to do something like this to streamline the query?

char[] array = new char[] { 'ア', 'イ', 'ウ', 'エ', 'オ' };

var query = DataContext.TenantDataServerTables.Where(p => 
                 p.Nursing_Home_Section == homeSection &&
                 p.Tenant_Kana_Last.ToString().StartsWith(array));

This is just an example as there are many more characters that I have to check StartsWith on the LINQ query.

Upvotes: 2

Views: 7086

Answers (6)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

You can use your array of "starts" in Any predicate, like this:

var array = new string[] {@"ア", @"イ", @"ウ", @"エ", @"オ"};
var query = DataContext.TenantDataServerTables.Where(p => 
    p.Nursing_Home_Section == homeSection &&
    array.Any(prefix => p.Tenant_Kana_Last.ToString().StartsWith(prefix))
);

Upvotes: 7

Duncan Carr
Duncan Carr

Reputation: 250

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            var searchList = new List<string> { "ア", "イ", "ウ", "エ", "オ" };

            var fullNameList = new List<string>
            {
                "Alpha",
                "アBravo",
                "Charlie",
                "イDelta",
                "Echo",
                "エFoxtrot",
                "Golf"
            };

            var finds = from name in fullNameList
                        from firstName in searchList
                        where name.StartsWith(firstName)
                        select name;

            foreach (string str in finds)
                Console.WriteLine(str);

            Console.ReadKey();
        }
    }
}

Upvotes: 4

Ronan Thibaudau
Ronan Thibaudau

Reputation: 3603

char[] array = new char[] { 'ア', 'イ', 'ウ', 'エ', 'オ' };
var query = DataContext.TenantDataServerTables
    .Where(p => p.Nursing_Home_Section == homeSection 
                && array
                    .Any(c=>p.Tenant_Kana_Last.ToString().StartsWith(c));

Any returns true if at least 1 matches the Truth, will work like your chained or operators

Upvotes: 3

qxg
qxg

Reputation: 7036

You can replace

p.Tenant_Kana_Last.ToString().StartsWith(array)

with

array.Any( c => p.Tenant_Kana_Last.ToString().StartsWith(c))

Upvotes: 3

Lukas K&#246;rfer
Lukas K&#246;rfer

Reputation: 14543

If you want to check for chars only, you can use HimBromBeere's approach. If the array should contain strings with more than one character, try this:

var query = DataContext.TenantDataServerTables.Where(p =>
    p.Nursing_Home_Section == homeSection &&
    array.Any(str => p.Tenant_Kana_Last.ToString().StartsWith(str)));

Upvotes: 5

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37113

As every string is nothing but an array of characters you could do any linq on it. Just use the very first character and check if it part of your array using Contains:

var query = DataContext.TenantDataServerTables.Where(p => 
             p.Nursing_Home_Section == homeSection &&
             array.Contains(p.Tenant_Kana_Last.ToString()[0]));

Alternativly to p.Tenant_Kana_Last.ToString()[0] you can also use p.Tenant_Kana_Last.First() which is a bit easier to read.

Upvotes: 5

Related Questions