Milad Rashidi
Milad Rashidi

Reputation: 1376

Constructing a list from an IOrderedQueryable by ToList method of linq

I provided this bellow query in a method of a service class of my Asp.Net MVC Application:

var query =
            (from company in _companyRepository.DbSet()
             join ct in _companyTagRepository.DbSet()
                 on company.CompanyID equals ct.CompanyID into cct
             from companyTag in cct.DefaultIfEmpty()
             where
                 filterItem.SelectedCompanyId.HasValue ? (company.CompanyID == filterItem.SelectedCompanyId.Value && company.CompanyStatusID == (int)Common.Enums.Status.Active) :
                 ((!filterItem.SelectedIndustries.Any() ||
                  filterItem.SelectedIndustries.Contains(companyTag.TagID.Value)) && company.CompanyStatusID == (int)Common.Enums.Status.Active &&
                 (companyTag != null && targetTagCategories.Contains(companyTag.Tag.TagCategoryID)))
             orderby company.CompanyID
             select new
             {
                 Id = company.CompanyID,
                 Title = company.CompanyFaName,
                 TitleEn = company.CompanyEnName,
                 CompanyBrands = !string.IsNullOrEmpty(company.CompanyFaName) ? company.CompanyFaBrands : company.CompanyEnBarnds,
                 CompanyProductsOrServicesFa = company.CompanyFaProductsOrServices,
                 CompanyProductsOrServicesEn = company.CompanyEnProductsOrServices,
                 WebSite = company.WebsiteAddress,
                 CompanyIcon = company.BackgroundImage,
                 company.CompanyTags
             })
            .GroupBy(company => new
            {
                company.Id,
                company.Title,
                company.TitleEn,
                company.CompanyBrands,
                company.CompanyProductsOrServicesFa,
                company.CompanyProductsOrServicesEn,
                company.WebSite,
            }, result => new
            {
                Tags = result.CompanyTags.Select(s => new
                {
                    CategryId = s.Tag.TagCategoryID,
                    Title = !string.IsNullOrEmpty(s.Company.CompanyFaName) ? s.Tag.TagFaName : s.Tag.TagEnName
                })
            })
            .OrderByDescending(o => o.Key.Id);

The Type of "query" variable is IOrderedQueryable and I can change output type of my method to IQueryable also.

My problem is where I trying to convert this query to a list in my controller. The compiler suggest me to use ToListAsync() method. Why I can't use ToList() method here?

Also I should explain that if I create the list in my service method, where is generating that query I will be able to use ToList() method but I allowed pass IQueryable or IOrderedQueryable to controller only.

Should I change type of my service method output from IQueryable or IOrderedQueryable to another types?

Upvotes: 0

Views: 2565

Answers (1)

Milad Rashidi
Milad Rashidi

Reputation: 1376

I found the solution. I must change the method output type from IOrderedQueryable to IOrderedQueryable<MyModel>

Upvotes: 2

Related Questions