9

yönteminden önce çağrılmalıdır. 'u MvcjQgrid kullanarak kullanmaya çalışıyordum ve bu özel durumu anladım.'OrderBy' yöntemi, 'Atlama' Özel Durum

System.NotSupportedException was unhandled by user code 
    Message=The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'. 

o istisna oluşturuyor neden olsa OrdeyBy atla yönteminden önce kullanılır? Nasıl çözülebilir?

ben denetleyicisi durum ile karşılaştı:

public ActionResult GridDataBasic(GridSettings gridSettings) 
     {   
      var jobdescription = sm.GetJobDescription(gridSettings); 
      var totalJobDescription = sm.CountJobDescription(gridSettings); 

      var jsonData = new 
      { 
       total = totalJobDescription/gridSettings.PageSize + 1, 
       page = gridSettings.PageIndex, 
       records = totalJobDescription, 
       rows = (
        from j in jobdescription 
        select new 
        { 
         id = j.JobDescriptionID, 
         cell = new[] 
        { 
         j.JobDescriptionID.ToString(), 
         j.JobTitle, 
         j.JobType.JobTypeName, 
         j.JobPriority.JobPriorityName, 
         j.JobType.Rate.ToString(), 
         j.CreationDate.ToShortDateString(), 
         j.JobDeadline.ToShortDateString(), 

        } 
        }).ToArray() 
      }; 
      return Json(jsonData, JsonRequestBehavior.AllowGet); 
     } 

GetJobDescription Yöntem ve CountJobDescription Yöntem

public int CountJobDescription(GridSettings gridSettings) 
     { 
      var jobdescription = _dataContext.JobDescriptions.AsQueryable(); 

      if (gridSettings.IsSearch) 
      { 
       jobdescription = gridSettings.Where.rules.Aggregate(jobdescription, FilterJobDescription); 
      } 
      return jobdescription.Count(); 
     } 

     public IQueryable<JobDescription> GetJobDescription(GridSettings gridSettings) 
     { 
      var jobdescription = orderJobDescription(_dataContext.JobDescriptions.AsQueryable(), gridSettings.SortColumn, gridSettings.SortOrder); 

      if (gridSettings.IsSearch) 
      { 
       jobdescription = gridSettings.Where.rules.Aggregate(jobdescription, FilterJobDescription); 
      } 

      return jobdescription.Skip((gridSettings.PageIndex - 1) * gridSettings.PageSize).Take(gridSettings.PageSize); 
     } 

Ve Nihayet FilterJobDescription ve OrderJobDescription

private static IQueryable<JobDescription> FilterJobDescription(IQueryable<JobDescription> jobdescriptions, Rule rule) 
     { 
      if (rule.field == "JobDescriptionID") 
      { 
       int result; 
       if (!int.TryParse(rule.data, out result)) 
        return jobdescriptions; 
       return jobdescriptions.Where(j => j.JobDescriptionID == Convert.ToInt32(rule.data)); 

      } 

// Similar Statements 

      return jobdescriptions; 
     } 



private IQueryable<JobDescription> orderJobDescription(IQueryable<JobDescription> jobdescriptions, string sortColumn, string sortOrder) 
     { 
      if (sortColumn == "JobDescriptionID") 
       return (sortOrder == "desc") ? jobdescriptions.OrderByDescending(j => j.JobDescriptionID) : jobdescriptions.OrderBy(j => j.JobDescriptionID); 

      return jobdescriptions; 
     } 
+0

Ama her zaman sipariş yok, sadece 'sortColumn == "JobDescriptionID" eğer', değil mi? – Slauma

+0

Fark eder mi? Bu arada, başka sipariş formlarım var. – nebula

cevap

14

Kural dışı durum, Skip numaralı uygulamayı uygularsanız, her zaman sıralı bir girişe ihtiyacınız olduğu anlamına gelir; ayrıca kullanıcının sıralamak için bir sütuna tıklamaması durumunda. Kullanıcıya bir sütun başlığını tıklayabilmeden önce ızgara görünümünü ilk kez açtığınızda hiçbir sıralama sütununun belirtilmediğini hayal edebiliyorum. Bu durumda yakalamak için ben örneğin, bazı varsayılan başka hiçbir sıralama kriter verildiğinde istediğiniz sıralama tanımlamak için öneririm:

switch (sortColumn) 
{ 
    case "JobDescriptionID": 
     return (sortOrder == "desc") 
      ? jobdescriptions.OrderByDescending(j => j.JobDescriptionID) 
      : jobdescriptions.OrderBy(j => j.JobDescriptionID); 

    case "JobDescriptionTitle": 
     return (sortOrder == "desc") 
      ? jobdescriptions.OrderByDescending(j => j.JobDescriptionTitle) 
      : jobdescriptions.OrderBy(j => j.JobDescriptionTitle); 

    // etc. 

    default: 
     return jobdescriptions.OrderBy(j => j.JobDescriptionID); 
} 

düzenleyin takip sorunlar hakkında

yorumunuza göre : Varlıklar sorgusuna LINQ içinde ToString() kullanamazsınız. Ve bir sonraki sorun, bir sorguda bir string dizisi oluşturamazsınız. Bazı kod adlı kullanarak sıraladıktan sonra sorunun aynı tür vardı

rows = (from j in jobdescription 
     select new 
     { 
      JobDescriptionID = j.JobDescriptionID, 
      JobTitle = j.JobTitle, 
      JobTypeName = j.JobType.JobTypeName, 
      JobPriorityName = j.JobPriority.JobPriorityName, 
      Rate = j.JobType.Rate, 
      CreationDate = j.CreationDate, 
      JobDeadline = j.JobDeadline 
     }) 
     .AsEnumerable() // DB query runs here, the rest is in memory 
     .Select(a => new 
     { 
      id = a.JobDescriptionID, 
      cell = new[] 
      { 
       a.JobDescriptionID.ToString(), 
       a.JobTitle, 
       a.JobTypeName, 
       a.JobPriorityName, 
       a.Rate.ToString(), 
       a.CreationDate.ToShortDateString(), 
       a.JobDeadline.ToShortDateString() 
      } 
     }) 
     .ToArray() 
+0

Bu istisna artık gitti. Ancak, denetleyicide LINQ, Varlıklar'ın 'System.String ToString()' yöntemini tanıyamadığı ve bu yöntemin bir mağaza ifadesine dönüştürülemeyeceği için yeni istisna ile karşılaşılır. 'JobDescriptionID' ve diğer öznitelikler nasıl dönüştürülür? dize o zaman? – nebula

+0

@aneal: Düzenle sayfama bakın. (Aslında asıl sorununla ilgisi yok.) – Slauma

3

: Ben bellekte dizeleri sonradan dönüştürmek (ve dize diziye) daha sonra kendi ana türleri ile DB'den veri yüklemek ve tavsiye ediyor OrderBy'de bir genel sıralama dizesini kabul eden Adam Anderson.

bu excpetion aldıktan sonra ben çok araştırma yaptım ve bu çok zeki düzeltme bulundu:

var query = SelectOrders(companyNo, sortExpression); 

return Queryable.Skip(query, iStartRow).Take(iPageSize).ToList(); 

Umut yardımcı olur!

SP

+0

Çözümümdeki genel uygulamalara sahibim, düzeltmeyi denedim ama işe yaramadı, bu arada mevcut değişimin ne yapacağı –

+0

@vishal: lütfen son yorumunuzu açıklığa kavuşturun ... – spilote