LoginSignup
1
2

More than 5 years have passed since last update.

C# > Linqの検索で、複数項目を検索する (Containsを使う)

Last updated at Posted at 2019-03-16

忘れるのでメモ

Whereのラムダ式の中で、Containsを使う

Listの例

public async static Task<List<Hoge>> GetBySerachListAsync(EntityContext context, List<string> serachList)
{
    return await context.Items.Where(x => serachList.Contains(x.value) ).ToListAsync();
}

HashSetの例

public async static Task<List<Hoge>> GetBySerachListAsync(EntityContext context, HashSet<string> serachList)
{
    return await context.Items.Where(x => serachList.Contains(x.value) ).ToListAsync();
}

補足

上記の説明でかなり端折ってますが、
EntityContextは、Microsoft.EntityFrameworkCore.DbContextから派生したものを想定してます。

ItemsはMicrosoft.EntityFrameworkCore.DbSetから派生してます。

以下のような感じです。

EntityContext.cs
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

public partial class EntityContext : DbContext
{
    public virtual DbSet<Hoge> Items { get; set; }
   ...
}

Hoge.cs
public partial class Hoge
{
    public long id { get; set; }
    public string value { get; set; }
}

追記

albireoさんのコメントによりHashSetの例を追加しました。
詳しくはコメント欄を。
albireoさんありがとうございます。

1
2
5

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
1
2