簡體   English   中英

使用存儲過程時,查詢結果不能被多次枚舉

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

我試圖用存儲過程替換視圖以基於關鍵字搜索結果。 但是當我傳遞關鍵字時會引發錯誤

查詢結果不能被多次枚舉

但如果將關鍵字保留為空,則效果很好。 以下是我獲取搜索結果的方法。 在這種情況下,誰能提供有關如何枚舉結果的建議?

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

最好先對查詢進行操作,然后最后返回可枚舉。

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