次のコードをリファクタリングしようとしました(一部のデータベースレコードをカテゴリと検索用語でフィルタリングします)。
from entry in _DB.TheTable
join otherEntry in _DB.AnotherTable
on entry.PrimaryKey equals otherEntry.Field
where category.Any(String.IsNullOrWhiteSpace)
|| category.Contains(entry.ProductGroup)
where String.IsNullOrWhiteSpace(searchTerm)
|| entry.ProductNumber.Contains(searchTerm)
|| otherEntry.Name1.Contains(searchTerm)
|| otherEntry.Name2.Contains(searchTerm)
|| otherEntry.Name3.Contains(searchTerm)
select new {entry.Something, entry.SomethingElse};
このように見えるように:
from entry in _DB.TheTable
join otherEntry in _DB.AnotherTable on entry.PrimaryKey equals otherEntry.Field
let searchFields = new[]{entry.ProductNumber, otherEntry.Name1, otherEntry.Name2,/*...*/}
where category.Any(String.IsNullOrWhiteSpace)
|| category.Contains(entry.ProductGroup)
where String.IsNullOrWhiteSpace(searchTerm)
|| searchFields.Any(field=>field.Contains(searchTerm))
select new {entry.Something, entry.SomethingElse};
残念ながら、LINQ to SQLはこれを実行できないようです。ArgumentExceptionコードから、次のようにsearchFields.Any(field=>field.Contains(searchTerm))記述されています。
引数
valueのタイプが間違っていました。期待:System.String.実際:System.String[].
これを機能するように調整するにはどうすればよいですか?
編集:申し訳ありませんが、言及するのを忘れたのsearchTermはstring、categoryですstring[]。