locked
Help Needed with a Query RRS feed

  • Question

  • User53449276 posted

    Hi All,

    I created a web api with a linq query. I am passing a comma delimited query string in URL, &campus=A,B,C. There is a special case for campus C, it needs to query a different column "Section". The results work well with campus A, B in the query string. But every time when I pass campus A,B, C, it only returns result C. Below is my query:

                        string[] items = campus.Split(',');
                         foreach (var item in items)
                        {
                            
                            if (item == "C" )
                            {
                               
                                 query = query.Where(a => a.SECTION.StartsWith("E"));
                                
                            }
                            else
                            {
                                query = query.Where(a => items.Contains(a.Campus));
                              
                            }
    
                        }

    What's wrong with my query? Thanks.

    Monday, February 27, 2017 9:10 PM

Answers

  • User53449276 posted

    Never mind. I figured it out. Need to move the special case Campus=="C" out of the foreach loop to compare. So the query will be:

                           if (campus == "C" )
                            {
                               
                                 query = query.Where(a => a.SECTION.StartsWith("E"));
                                
                            } 
                        string[] items = campus.Split(',');
    
                         foreach (var item in items)
                        {
                            
                          
                            
                                query = query.Where(a => items.Contains(a.Campus));
                              
                           
    
                        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 27, 2017 9:34 PM