MappedItemクラスのリストを生成しようとすると、エラーが発生します。以下を参照してください。簡単に言うと、以下のコード例では、カテゴリ、日付範囲、SKUで商品を検索しようとしています。私が持っている要件は、ユーザーがSKUのコンマ区切りリストを入力できる必要があり、検索は、ユーザーが入力したSKUの1つでSKUが始まる製品を見つけることです。コードを実行すると、次のようになります。
ローカルシーケンスは、Contains()演算子を除くクエリ演算子のLINQtoSQL実装では使用できません。
省略されたシーケンスは次のとおりです。
SKUのコンマ区切り文字列を文字列のリストに変換します。
string sku = TextSKU.Text;
List<string> skuList = sku.Split(new char[] { ',' }).ToList();
コードの他の場所で、検索結果を受け入れるクラスを定義します。
public class MappedItem
{
public string ItemDescription { get; set; }
public int ItemCount { get; set; }
public MappedItem()
{
}
public MappedItem(string itemDescription, int itemCount)
{
ItemDescription = itemDescription;
ItemCount = itemCount;
}
}
これが私の結果を生成するクエリです
List<MappedItem> widgetItems = (from c1 in db.CCRCodes
join pac in db.widgetAssignedCodes on c1.code_id equals pac.code_id
join ph in db.widgetHistories on pac.history_id equals ph.history_id
where ph.contact_dt.Value.Date >= startDate && ph.contact_dt.Value.Date <= endDate &&
(string.IsNullOrEmpty(baanCatFam) || ph.baan_cat_family_code == baanCatFam) &&
(string.IsNullOrEmpty(baanCat) || ph.baan_cat_code == baanCat) &&
(string.IsNullOrEmpty(baanSubCat) || (ph.baan_sub_cat_code == baanSubCat)) &&
(string.IsNullOrEmpty(sku) || skuList.All(sl => ph.product_mod.StartsWith(sl)))
group c1 by c1.code_desc into ct
select new MappedItem
{
ItemDescription = ct.Key.ToUpper(),
ItemCount = ct.Count()
}).OrderByDescending(m => m.ItemCount)
.ToList();
犯人は、私が抽出して以下に表示したコード行であると思います。
skuList.All(sl => ph.product_mod.StartsWith(sl))
これは、ユーザーが入力したskusのコンマ区切りリストから派生したskuListの要素で始まるすべてのskusを識別します。私の質問は、このエラーの原因と、コード例を前提として、それらを回避するために何をするかです。