我按照以下链接中的教程在网页上实现了排序,过滤和分页
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-2.2
排序和分页工作正常,但搜索功能出现问题。我收到错误“无法将类型'System.Linq.IQueryable'隐式转换为'System.Linq.IOrderedQueryable'”。有谁可以帮助我吗。预先感谢
我在这里有我的Model.Profile
top
这是我的控制器上的代码抛出错误
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; }
}
}
答案 0 :(得分:0)
变量配置文件已经声明为IOrderedQueryable,并且您尝试分配IQueryable。
如果尝试会怎样:
if (!string.IsNullOrEmpty (searchData)) {
var profile2 = profile.Where (p =>
p.ProfileName.Contains (searchData)); //Here is the error
}
答案 1 :(得分:0)
应该是一个简单的解决方案。使用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,向您展示了我刚才写的一个示例。它按预期工作。
让我知道这是否可行。
答案 2 :(得分:0)
首先获得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));
}
答案 3 :(得分:0)
谢谢邹兴!
昨天,我还尝试在数据集上添加IQueryable,如下所示,它可以正常工作。当我看到您的答案时,我尝试了一下,它也正在起作用。感谢您的帮助,这两个实现工作正常:)
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));
}