【问题标题】:Linq does not contain the definition for 'Where'Linq 不包含“Where”的定义
【发布时间】:2014-03-31 16:44:59
【问题描述】:

我正在尝试通过如下所示的 if 语句调整我的查询:

        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;

        }

这似乎没有给我任何错误。但是,当我尝试使用 where 子句进行更多过滤时,它无法识别该术语。我知道 IQuerable 不支持它,但是有没有办法将“文章”最初设置为 null,然后用 if 语句设置它?

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

我知道我可以在一个大的 if 语句中执行此操作 if(User.IsInRole("Admin")) 然后执行所有代码,然后将相同的代码复制并粘贴到不同的 if 语句中 (if(user.IsInRole("Educator )),但我认为这是多余且非常糟糕的编码实践。

干杯。

【问题讨论】:

    标签: c# sql linq


    【解决方案1】:

    您的articles 变量正在使用非泛型类型IQueryable,它确实支持的很少。

    您需要IQueryable<T> 来获得合适的<T>。例如:

    IQueryable<Article> articles;
    // Initialize as before
    

    不过,我会亲自更改您的初始化方式:

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

    【讨论】:

    • 你会重构这个开关吗?
    • @Areks:可能。我可能会将其重构为 Dictionary&lt;string, Func&lt;IQueryable&lt;Article&gt;, IQueryable&lt;Article&gt;&gt;&gt;
    猜你喜欢
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多