当我尝试生成 MappedItem 类的列表时,出现错误,请参见下文。简而言之,下面的代码示例尝试按类别、日期范围和 SKU 查找产品。我的要求是用户应该能够输入逗号分隔的 SKU 列表,并且搜索是找到 SKU 以用户输入的 SKU 之一开头的任何产品。当我运行代码时,我得到了。
本地序列不能在查询运算符的 LINQ to SQL 实现中使用,但 Contains() 运算符除外。
缩写顺序是这样的:
将逗号分隔的 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))
这标识了所有以 skuList 中的元素开头的 sku,该元素源自用户输入的以逗号分隔的 sku 列表。我的问题是,是什么导致了这个错误,并且给出了代码示例,我该怎么做才能绕过它们。