Linq does not contain the definition for 'Where'

I'm trying to adjust my query through an if statement as shown below:

        IQueryable articles = null;

        if (User.IsInRole("Admin"))
        {
            articles = from s in db.Articles
                       select s;
        }
        if (User.IsInRole("Educator"))
        {
            articles = from s in db.Articles
                       where s.createdBy == WebSecurity.CurrentUserName
                       select s;

        }

This doesn't seem to give me any errors. However, when I try to filter a bit more with a where clause it doesn't recognize the term. I understand IQuerable doesn't support it, but is there a way to originally set "articles" to null, then set it with a if statement?

if (!String.IsNullOrEmpty(searchString))
        {
            articles = articles.Where(s => s.title.ToUpper().Contains(searchString.ToUpper())
                                   || s.content.ToUpper().Contains(searchString.ToUpper()));
        }
        switch (sortOrder)
        {
            case "name_desc":
                articles = articles.OrderByDescending(s => s.title);
                break;
            case "Date":
                articles = articles.OrderBy(s => s.dateCreated);
                break;
            case "date_desc":
                articles = articles.OrderByDescending(s => s.dateCreated);
                break;
            case "rating_desc":
                articles = articles.OrderByDescending(s => s.finalReview);
                break;
            case "Rating":
                articles = articles.OrderBy(s => s.finalReview);
                break;
            case "Views":
                articles = articles.OrderBy(s=>s.numViews);
                break;
            case "views_desc":
                articles = articles.OrderByDescending(s => s.numViews);
                break;
            case "Educators":
                articles = articles.OrderBy(s => s.educatorCRUD);
                break;
            case "educators_desc":
                articles = articles.OrderByDescending(s => s.educatorCRUD);
                break;
            default:
                articles = articles.OrderBy(s => s.title);
                break;
        }

I know I can do this in a big if statment if(User.IsInRole("Admin")) then execute all code then copy and paste the same code in a different if statement (if(user.IsInRole("Educator)), but I think this redundant and really bad coding practice.

Cheers.

Jon Skeet
people
quotationmark

Your articles variable is using the non-generic type IQueryable, which supports very little indeed.

You want IQueryable<T> for a suitable <T>. For example:

IQueryable<Article> articles;
// Initialize as before

I'd personally change how you're initializing it though:

IQueryable<Article> articles = db.Articles;
if (User.IsInRole("Admin"))
{
    // No change...
}
else if (User.IsInRole("Educator"))
{
    articles = articles.Where(s => s.createdBy == WebSecurity.CurrentUserName);
}
else
{
    // Throw an exception? What do you want to happen if they're neither an
    // educator nor an administrator?
}

people

See more on this question at Stackoverflow