无法将类型 'System.Linq.IQueryable<Models.Profile>' 隐式转换为'System.Linq.IOrderedQueryable<Model



我按照以下链接中的教程在我的网页上实现了排序、过滤、分页

https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-2.2

排序和分页工作正常,但我在搜索功能上遇到了问题。我收到错误"无法隐式转换tyoe'System.Linq.IQueryable'到'System.Linq.IOrderedQueryable'"。有人可以帮我吗?提前致谢

我这里有我的模型配置文件

public partial class Profile
{
    public Profile()
    {
        Assessment = new HashSet<Assessment>();
    }
    public int ProfileId { get; set; }
    [DisplayName ("Profile Name")]
    public string ProfileName { get; set; }
    [DisplayName("Company Name")]
    public string CompanyName { get; set; }
    public int RoleId { get; set; }
    public int IndustryId { get; set; }
    public int PerspectiveId { get; set; }
    public int InfluenceLevelId { get; set; }
    public int OwnershipLevelId { get; set; }
    public string Interviewer { get; set; }
    [DisplayName ("Date Interviewed")]
    public DateTime? DateInterviewed { get; set; }
    [DisplayName("Created By")]
    public string CreatedBy { get; set; }
    [DisplayName("Created Date")]
    public DateTime? CreatedDate { get; set; }
    [DisplayName("Modified By")]
    public string ModifiedBy { get; set; }
    [DisplayName("Modifed Date")]
    public DateTime? ModifiedDate { get; set; }
    public virtual Industry Industry { get; set; }
    [DisplayName ("Influence Level")]
    public virtual InfluenceLevel InfluenceLevel { get; set; }
    [DisplayName ("Ownership Level")]
    public virtual OwnershipLevel OwnershipLevel { get; set; }
    public virtual Perspective Perspective { get; set; }
    public virtual Role Role { get; set; }
    public virtual ICollection<Assessment> Assessment { get; set; }
}
}

这是我在控制器上的代码,它抛出了错误

{
    ViewData["CurrentFilter"] = searchData;
    var profile = _context.Profile
        .Include (p => p.Industry)
        .Include (p => p.InfluenceLevel)
        .Include (p => p.OwnershipLevel)
        .Include (p => p.Perspective)
        .Include (p => p.Role)
        .OrderByDescending (p => p.ProfileName);
    if (!string.IsNullOrEmpty (searchData)) {
        profile = profile.Where (p =>
            p.ProfileName.Contains (searchData)); //Here is the error
    }

首先会得到IOrderedQueryable类型的profile,然后为其分配IQueryable类型的日期profile.Where (p =>p.ProfileName.Contains (searchData));

尝试使用以下代码将profile设置为IQueryable

var profile = from p in _context.Profile
              select p;
 if (!string.IsNullOrEmpty (searchData)) {
    profile = profile
       .Include (p => p.Industry)
       .Include (p => p.InfluenceLevel)
       .Include (p => p.OwnershipLevel)
       .Include (p => p.Perspective)
       .Include (p => p.Role)
       .OrderByDescending (p => p.ProfileName)
       .Where (p =>p.ProfileName.Contains (searchData));
}         
应该是

一个简单的解决方案。 使用 LINQ 时,可以使用 .ToList() 方法。 因此,对于您的代码:

var profile = _context.Profile
    .Include (p => p.Industry)
    .Include (p => p.InfluenceLevel)
    .Include (p => p.OwnershipLevel)
    .Include (p => p.Perspective)
    .Include (p => p.Role)
    .OrderByDescending (p => p.ProfileName)
    .ToList(); // add here
if (!string.IsNullOrEmpty (searchData)) {
    profile = profile.Where (p =>
        p.ProfileName.Contains (searchData)).ToList(); // add here
}

我创建了一个DotNetFiddle来向您展示我刚刚写的一个例子。 它正在按预期工作。

让我知道这是否有效。

变量配置文件已声明为 IOrderedQueryable,并且您尝试分配 IQueryable。

如果您尝试:

if (!string.IsNullOrEmpty (searchData)) {
    var profile2 = profile.Where (p =>
        p.ProfileName.Contains (searchData)); //Here is the error
}

谢谢邹邢!

昨天我还尝试在我的数据集上添加 IQueryable,如下所示,它可以工作。当我看到你的答案时,我尝试了它,它也有效。感谢您的帮助,2 个实现工作:)

        IQueryable<Profile> profile = _context.Profile
            .Include(p => p.Industry)
            .Include(p => p.InfluenceLevel)
            .Include(p => p.OwnershipLevel)
            .Include(p => p.Perspective)
            .Include(p => p.Role)
            .OrderByDescending(p => p.ProfileName);

        if (!string.IsNullOrEmpty(searchData))
        {
            profile = profile.Where(p => p.ProfileName.Contains(searchData));
        }

相关内容

最新更新