CS0266无法转换类型

时间:2019-11-29 08:53:26

标签: c# asp.net asp.net-mvc entity-framework

我有一个错误:

  

CS0266 C#无法隐式转换类型   'System.Linq.IQueryable'到   'System.Linq.IOrderedIQueryable'。一个   存在显式转换(您是否缺少演员表?)

这是我的控制人:

public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
    ViewBag.CurrentSort = sortOrder;
    ViewBag.NameSurnameSortParm = sortOrder == "NameSurname" ? "NameSurname_desc" : "NameSurname";
    ViewBag.ReasonSortParm = sortOrder == "Reason" ? "Reason_desc" : "Reason";
    ViewBag.AccessSortParm = sortOrder == "Access" ? "Access_desc" : "Access";
    ViewBag.UserOrAdminSortParm = sortOrder == "UserOrAdmin" ? "UserOrAdmin_desc" : "UserOrAdmin";

    ViewBag.DepartmentSortParm = sortOrder == "Department" ? "Department_desc" : "Department";
    ViewBag.UNCPathSortParm = sortOrder == "UNCPath" ? "UNCPath_desc" : "UNCPath";

    if (searchString != null)
    {
        page = 1;
    }

    else
    {
        searchString = currentFilter;
    }

    ViewBag.CurrentFilter = searchString;

    var request = from c in _context.RaidRequest
                    orderby c.Id
                    select c;

    if (!String.IsNullOrEmpty(searchString))
    {
        request = request.Where(s => s.NameSurname.Contains(searchString));
    }

    switch (sortOrder)
    {
        case "NameSurname_desc":
            request = request.OrderByDescending(c => c.NameSurname);
            break;
        case "Reason":
            request = request.OrderBy(c => c.Reason);
            break;
        case "Reason_desc":
            request = request.OrderByDescending(c => c.Reason);
            break;
        case "Access":
            request = request.OrderBy(c => c.Access);
            break;
        case "Access_desc":
            request = request.OrderByDescending(c => c.Access);
            break;
        case "UserOrAdmin":
            request = request.OrderBy(c => c.UserOrAdmin);
            break;
        case "UserOrAdmin_desc":
            request = request.OrderByDescending(c => c.UserOrAdmin);
            break;
        case "Department":
            request = request.OrderBy(c => c.Department);
            break;
        case "Department_desc":
            request = request.OrderByDescending(c => c.Department);
            break;
        case "UNCPath":
            request = request.OrderBy(c => c.UNCPath);
            break;
        case "UNCPath_desc":
            request = request.OrderByDescending(c => c.UNCPath);
            break;
    }

    int pageSize = 10;
    int pageNumber = (page ?? 1);
    return View(request.ToPagedList(pageNumber, pageSize));
}

这部分损坏了:

if (!String.IsNullOrEmpty(searchString))
{
    request = request.Where(s => s.NameSurname.Contains(searchString));
}

请您解释一下该怎么做? 我逐字地跟随了教程。一直有效,直到我添加了分页功能。 可能与我的观点有关吗?

1 个答案:

答案 0 :(得分:1)

您的所有查询都包含一个orderby,因此返回 const subj='<div class="item"><span class="label">Test:</span><span>server</span></div><div class="item"><span><span class="label">Assigned to:</span><span>Fred(position) </span></div><div'; const re=/Assigned to:<\/span><span>([^\(]*)/; const answers=re.exec(subj); console.log(answers[1]); ,但您使用搜索字符串的查询除外。该查询返回一个IOrderedQueryable

由于IQueryable是从IOrderedQueryable继承的,因此您可以将IQueryable分配给IOrderedQueryable,但不能相反。

从第一个查询中删除orderby,它将变为IQueryable,向switch语句添加IQueryable以进行默认排序。这也将防止您对结果查询进行两次排序。

default: