我有一个成功的查询,它将两个表与 where 和 orderby 子句联系起来,但我想添加以仅选择特定列而不是取回所有内容。
第 1 部分当我尝试这样做时,我在 orderby 行上出现语法错误,如果我删除 orderby 行,语法错误将移至 where 行。
错误 3 无法将类型“System.Linq.IOrderedQueryable”隐式转换为“System.Linq.IQueryable”。存在显式转换(您是否缺少演员表?)
IQueryable<VendorProfile> query = _db.VendorProfiles
.Include("VendorCategories")
.Include("VendorsSelected")
.Select(s => new { s.ProfileID, s.Name, s.CompanyName, s.City, s.State, s.DateCreated, s.VendorsSelected, s.VendorCategories })
.Where(x => x.VendorsSelected.Select(s => s.UserName).Contains(HttpContext.Current.User.Identity.Name))
.OrderBy(x => x.DateCreated);
if (criteria.name != string.Empty)
query = query.Where(v => v.Name.Contains(criteria.name));
if (criteria.company != string.Empty)
query = query.Where(v => v.CompanyName.Contains(criteria.company));
if (criteria.startDate != null && criteria.endDate != null)
query = query.Where(v => v.DateCreated > criteria.startDate && v.DateCreated < criteria.endDate);
if (criteria.categories != null && !criteria.categoryMatchAll)
query = query.Where(v => criteria.categories.AsQueryable().Any(cat => v.VendorCategories.Select(vendCat => vendCat.CategoryID).Contains(cat)));
if (criteria.categories != null && criteria.categoryMatchAll)
query = query.Where(v => criteria.categories.AsQueryable().All(cat => v.VendorCategories.Select(vendCat => vendCat.CategoryID).Contains(cat)));
if (criteria.minorityType != null)
query = query.Where(v => v.MinotiryOwned == criteria.minorityType);
if (criteria.diversityClass != null)
query = query.Where(v => v.DiversityClassification == criteria.diversityClass);
return query.ToList();
第 2 部分我还想知道是否可以将选定的列提取到视图模型类中,所以我厌倦了这个,我在 orderby 行上得到了与上面相同的结果
错误 4 无法将类型“System.Linq.IOrderedQueryable”隐式转换为“System.Linq.IQueryable”。存在显式转换(您是否缺少演员表?)