简体   繁体   English

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

[英]The result of a query cannot be enumerated more than once when using stored procedure

I am trying to replace a view with a stored procedure to search results based on a keyword. 我试图用存储过程替换视图以基于关键字搜索结果。 But when I pass a keyword it throws an error 但是当我传递关键字时会引发错误

The result of a query cannot be enumerated more than once 查询结果不能被多次枚举

but it works fine if the keyword is left empty. 但如果将关键字保留为空,则效果很好。 Below is my method to get search results. 以下是我获取搜索结果的方法。 Can anyone provide any suggestions on how to enumerate the results in this case? 在这种情况下,谁能提供有关如何枚举结果的建议?

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;
}

Its better to first operate on the Query and then return the enumerable at last. 最好先对查询进行操作,然后最后返回可枚举。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM