I have a successful query that links two tables with a where and orderby clause, but I wanted to add to just select specific columns instead of getting everything back.
PART 1 When I attempt this I get syntax errors on the orderby line, if I remove the orderby line the syntax errors move to the where line.
Error 3 Cannot implicitly convert type 'System.Linq.IOrderedQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)
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();
PART 2 I also wanted to know if I could extract the selected columns into a view model class, so I tired this and I get same results as above on the orderby line
Error 4 Cannot implicitly convert type 'System.Linq.IOrderedQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)