views:

35

answers:

2

Hi All,

I have a bit of code that works with a result set called "result" (original I know) but depending on the incoming variable I'd like to fire the specific query depending. I have the below in an if statement but that just makes the "result" recordset gets those nasty red lines and it doesn't work. I'm sure this is easy to work out.

 if (area == "dashboard")
     {
         IQueryable<ViewGetNavigationMainItem> result = (from m in 
             _entities.ViewGetNavigationMainItems
             where m.area.Trim() == area.Trim()
             where m.state == "Online"
             where m.parentID == parentID
             orderby m.sequence ascending
             select m);            
     }
 else
     {
         //Get the Content Items
         IQueryable<ViewGetNavigationContentItem> result = (from m in 
             _entities.ViewGetNavigationContentItems
             where m.navID == navID
             where m.parentID == parentID
             orderby m.contentOrder ascending
             select m);
     }
maxRecords = result.Count();

 foreach (var item in result)
 {
     //do something
 }
+1  A: 

Note: You can use && instead of multiple where.

First, define result before the if as an IQueryable to the base class or interface of the two types of results (in C# 4.0) or as an Enumerable. You can create such a base class. Let's say that the only common base class for ViewGetNavigationMainItem and ViewGetNavigationContentItem is Object:

 IQueryable<object> result;

 if (area == "dashboard")
                        {
                            result = (from m in _entities.ViewGetNavigationMainItems
                                      where m.area.Trim() == area.Trim()
                                      && m.state == "Online"
                                      && m.parentID == parentID
                                      orderby m.sequence ascending
                                      select m);  
                        }
                        else
                        {
                            //Get the Content Items
                            result = (from m in _entities.ViewGetNavigationContentItems
                                      where m.navID == navID
                                      && m.parentID == parentID
                                      orderby m.contentOrder ascending
                                      select m);
                        }

In the foreach, instead of var use the base class (or common interface) for both ViewGetNavigationMainItem and ViewGetNavigationContentItem. If you don't have a more specific base class than Object use object.

foreach (object item in result)
                        { etc etc etc
brickner
+1  A: 

in your code, "result" is not in scope when you check the count as it is declared in the "if" and "else"

you need to move the declaration of result above the if, and declare it as something that both the results are, eg IEnumerable.

if all you want to do is count them that is...

if you want to do more, consider creating a model which you can select both results into, e.g.

class ResultModel{
 int Id{get;set;}
 string Display{get;set;}
}
Anthony Johnston