gpt4 book ai didi

c# - 使用存储过程时,不能多次枚举查询结果

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:34 24 4
gpt4 key购买 nike

我正在尝试用存储过程替换 View 以根据关键字搜索结果。但是当我传递一个关键字时它会抛出一个错误

The result of a query cannot be enumerated more than once

但如果关键字留空,它会正常工作。以下是我获取搜索结果的方法。谁能就在这种情况下如何枚举结果提供任何建议?

public IEnumerable <BrandNameToIngredient> GetBrandNameToIngMapResults(string Keyword)
{
IEnumerable<BrandNameToIngredient> lstBrandNametoIng = from map in DB.USP_BRANDNAME_INGREDIENT_MAP()
select new BrandNameToIngredient
{
IngredientBrandNameMapID=map.INGREDIENT_PRODUCT_MAP_ID,
BrandName = map.FDA_BRAND_NAME, //From Table 1
PFCName = map.PFC_DESC==null?"":map.PFC_DESC, //From Table 1
IngredientName = map.INGREDIENT_NAME, //From Table 2
HCIngredientName = map.HC_INGREDIENT_NAME, //From Table 2
KeywordfromPage = Keyword
};

if (!string.IsNullOrEmpty(Keyword))
{
lstBrandNametoIng = lstBrandNametoIng.Where(x => x.BrandName.ToLower().Contains(x.KeywordfromPage.ToLower()) //Able to get result
|| x.PFCName.ToLower().Contains(x.KeywordfromPage.ToLower()) //Able to get result

|| x.IngredientName.ToLower().Contains(x.KeywordfromPage.ToLower()) //Error Here
|| x.HCIngredientName.ToLower().Contains(x.KeywordfromPage.ToLower())); //Error Here
}

return lstBrandNametoIng;
}

最佳答案

最好先对Query进行操作,最后再返回可枚举。

public IEnumerable<BrandNameToIngredient> GetBrandNameToIngMapResults(string Keyword)
{
var brandNametoIngQuery = DB.USP_BRANDNAME_INGREDIENT_MAP()
.Where(x => string.IsNullOrEmpty(Keyword)
|| x.BrandName.Contains(Keyword, StringComparison.OrdinalIgnoreCase)
|| x.PFCName.Contains(Keyword, StringComparison.OrdinalIgnoreCase)
|| x.IngredientName.Contains(Keyword, StringComparison.OrdinalIgnoreCase)
|| x.HCIngredientName.Contains(Keyword, StringComparison.OrdinalIgnoreCase))
.Select(map=> new BrandNameToIngredient
{
IngredientBrandNameMapID = map.INGREDIENT_PRODUCT_MAP_ID,
BrandName = map.FDA_BRAND_NAME, //From Table 1
PFCName = map.PFC_DESC == null ? "" : map.PFC_DESC, //From Table 1
IngredientName = map.INGREDIENT_NAME, //From Table 2
HCIngredientName = map.HC_INGREDIENT_NAME, //From Table 2
KeywordfromPage = Keyword
}).AsEnumerable();
}

关于c# - 使用存储过程时,不能多次枚举查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com