2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EntityFramework 6ではSqlFunctions.PatIndexだけどEntityFramework Coreでのパターン検索は?

Last updated at Posted at 2021-02-16

はじめに

たとえば顧客リストを表示する画面があったとして、顧客を五十音順で検索できるようにしたいということがあります。
こんな画面です
image.png

.Net Framework 4.8でEntity Framework6を使っていましたが、このときは以下のようなコードを書いていました。

   using System.Data.Entity.SqlServer;

   var query = dbContext.Customers;

   //五十順
    string pattern = string.Empty;
    switch (kanaState)
    {
        case KanaButtonValue.A:
            {
                pattern = "[ア-オ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.KA:
            {
                pattern = "[カ-ゴ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.SA:
            {
                pattern = "[サ-ゾ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.TA:
            {
                pattern = "[タ-ド]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.NA:
            {
                pattern = "[ナ-ノ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.HA:
            {
                pattern = "[ハ-ボ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.MA:
            {
                pattern = "[マ-モ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.YA:
            {
                pattern = "[ヤ-ヨ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.RA:
            {
                pattern = "[ラ-ロ]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.WA:
            {
                pattern = "[ワ-ン]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) > 0);
                break;
            }
        case KanaButtonValue.OTHER:
            {
                pattern = "[ア-ン]%";
                query = query.Where(x => SqlFunctions.PatIndex(pattern, x.CustomerNameKana) == 0);
                break;
            }
    }

SqlFunctions.PatIndexを使用して、パターン検索を行っていました。

.NET5にアプリをアップし、Entity Framework Coreを使用すると、SqlFunctions.PatIndexが使えませんでした

SqlFunctions.PatIndexはMicrosoftのドキュメントにあるように.NET Frameworkでしか使えないようです。
そのため、代替手段を考える必要が出てきました。

最初はStartWithやContainsが使えるかなと試したのですが、%といったワイルドカードが使用できなくなるみたいでうまくいきませんでした。
その後調べ続けたところ、見つけたのがこちら

EF.Functions.Like

EF Core 2.0 の新機能で紹介されていました。

var aCustomers =
    from c in context.Customers
    where EF.Functions.Like(c.Name, "a%")
    select c;

これを使うことで、パターン検索ができるみたいです。
変更したコードはこんな感じです

    pattern = "[ア-オ]%";
    query = query.Where(x => EF.Functions.Like(x.CustomerNameKana, pattern));

これで、以前と同じようにパターン検索が可能となりました。
EF Coreを触ったのが初めてだったので、いろいろとまだまだ知らないことが多そうです。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?