2017-01-17 5 views
-1

別のメインカテゴリをカウント順に降順に選択しようとしています。しかし、次のエラーを取得:ここラムダ式を使用して異なる値を順番にソート

DbExpressionBinding requires an input expression with a collection ResultType.

Parameter name: input

は私のコードです:

var Categories = products.Where(p => p.IsActive == true) 
    .Select(n => n.MainCategory).Distinct().ToArray().OrderByDescending(p => p.Count()); 
if (Categories != null) 
{ 
    int j = 0; 
    foreach (var Category in Categories) 
    { 
     j++; 
     var brands = products.Where(p => p.IsActive == true && p.MainCategory == Category && p.Brand != string.Empty && p.Brand != null).Select(n => n.Brand).Distinct().ToArray(); 
     if (brands == null || brands.Length == 0) 
     { 
      sb.AppendFormat("<li><a href=\"/hi-fi/{1}\" class=\"list-group-item list-group-item-success colordiv\">{0}</a><a href=\"#demo{2}\" data-toggle=\"collapse\" data-parent=\"#MainMenu\" class=\"glyphicon glyphicon-minus minus\"></a>\n", Category, Category.Replace(" ", "-"), j); 
     } 
     else 
     { 
      //code to list brands} 
     } 
    } 
} 

は私を導いてください。

+0

以下のようにあなたはaboves可能) 'の各グループ –

+0

@StephenMuecke他のクエリにも影響するので、GroupBy()を使用することはできません – user777

+0

他のどのようなクエリですか? 'var brands = products.Where(p => p.IsActive == true && p.MainCategory == Category && p.Brand!= string)というクエリには、 '.GroupBy()') –

答えて

0

あなたはこのリンクを読んでくださいhttps://msdn.microsoft.com/en-us/library/bb548916(v=vs.110).aspx
私はあなたの必要性の下で動作すると思います。

public class MainCategory 
{ 
    public IEnumerable<string> Datas { get; set; } 
} 

public class SpecialComprarer : IComparer<MainCategory> 
{ 
    public int Compare(MainCategory x, MainCategory y) 
    { 
     if (x.Datas.Count() == y.Datas.Count()) 
      return 0; 
     else if (x.Datas.Count() > y.Datas.Count()) 
      return 1; 
     else 
      return -1; 
    } 
}  

あなたは `.Countを(取得後、MainCategory`and`で貴社の製品グループに `.GroupByを()`を使用する必要があります

List<MainCategory> db = new List<MainCategory> { 

     new MainCategory { Datas = new List<string> { "1", "2", "3" } }, 
     new MainCategory { Datas = new List<string> { "1", "2", "3", "4" } }, 
     new MainCategory { Datas = new List<string> { "1", "2", "3", "4", "5" } }, 
     new MainCategory { Datas = new List<string> { "1", "2", "3", "4", "5", "6" } } 
     }; 
     List<MainCategory> x = db.OrderByDescending(m => m, new SpecialComprarer()).ToList(); 
関連する問題