2

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;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Programmermid
  • 588
  • 3
  • 9
  • 30

1 Answers1

0

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();
}
Harsh
  • 3,683
  • 2
  • 25
  • 41