2

私はこの会社のリスト(カテゴリに参加)を持っています。このリストでは、自由なテキスト入力で検索し、通常は名前/アルファベット順に結果を並べ替えます。

しかし、返された企業が特定のカテゴリにリンクされている場合 (これらのより重要なカテゴリの ID はリストにあります)、主にこれらのカテゴリの企業を上位に並べて結果を並べ替え、二次的な順序はアルファベット順にする必要があります。

この順序を追加するにはどうすればよいですか?

    List<int> importantCategories = new List<int>() { 14, 99, 4428 };

    var companies = (from c in context.Companies
                     join ct in context.Categories on c.CategoryId equals ct.CategoryId
                     where ct.Active == true 
                     && c.Name.Contains(freeTextFilter)
                     orderby c.Name
                       --- if company is from category 14, 99, 4428 
                           then the ordering should place those above the rest
                     select c);

クエリから数行を取り出しましたが、結合された Categories テーブルを使用しなかった理由の質問の後に、そのうちの 1 行を追加しました。

4

5 に答える 5

4

もしかしてこんな?

List<int> importantCategories = new List<int>() { 14, 99, 4428 };

var companies = (from c in context.Companies
                 join ct in context.Categories on c.CategoryId equals ct.CategoryId
                 where c.Name.Contains(freeTextFilter)
                 orderby importCategories.Contains(ct.CategoryID) ? 0 : 1, c.Name
                 select c);

または、カテゴリにも順序を付けたい場合は、これを試してください。

orderby importCategories.Contains(ct.CategoryID) ? ct.CategoryID : 4429, c.Name

4429 は、動的に計算できるリストの最大値 + 1 です。

提案されているように、これも機能しますが、categoryID で並べ替えたい場合は機能しません。

orderby !importCategories.Contains(ct.CategoryID) , c.Name

ct が使用されない理由はありますか?

于 2013-02-02T20:02:14.573 に答える
1

まず、実際にはカテゴリのデータを使用していないため、結合を行う必要はないと思います。フィルタリングにカテゴリ ID のみを使用しており、Companyそのデータが既に含まれています。次に、カテゴリ内でアルファベット順に取得できるようにOrderBy、カスタム キー セレクターと aを使用する必要があります。ThenByこれは流暢な構文を使用しているためです。

最初にカテゴリ ID で並べ替えますが、指定されたカテゴリにある場合にのみ、他のエントリは同じと見なされます ( int.MaxValue)。次にName、カテゴリ選択内で並べ替えます。最初のセクションのカテゴリの順序を気にしない場合はc => categories.Contains(c.CategoryId) ? 0 : 1、キー セレクターとして使用できます。

var categories = new int[] { 14, 99, 4428 };

var companies = context.Companies
                       .Where(c => c.Name.Contains(freeTextFilter))
                       .OrderBy(c = categories.Contains(c.CategoryId) 
                                        ? c.CategoryId 
                                        : int.MaxValue)
                       .ThenBy(c => c.Name);
于 2013-02-02T20:07:45.633 に答える
1

私は試してみます

orderby (importantCategories.Contains(ct.CatetoryId) 
                            ? 0
                            : 1),
         c.Name
于 2013-02-02T19:59:50.937 に答える
1

ThenBy を使用できます: ここに例を示します

 List<string> strings = new List<string> { "f", "a", "b", "c", "d", "e" };

        var result = strings.OrderByDescending(x => x == "e")
                            .ThenByDescending(x => x == "c")
                            .ThenBy(x=>x);

これにより、「e」、「c」、「a」、「b」、「d」、「f」が得られます

それを問題に適用して、正しい順序を取得できると思います。

于 2013-02-02T20:10:10.930 に答える
0

これは、クエリ構文ではなく拡張メソッドと Lambda 式のみを使用する Hogan の回答に似ています (ただし、これはおそらく、クエリ構文の方が見やすい場合です)。

var companies = context.Companies
    .Join(context.Categories, c => c.CategoryId, ct => ct.CategoryId, (c, ct) => c)
    .Where(c => c.Name.Contains(freeTextFilter))
    .OrderBy(c => importantCategories.Contains(c.CategoryId) ? 0 : 1)
    .ThenBy(c => c.Name);

あなたが本当に結合を必要としないかもしれないと仮定して...

var companies = context.Companies
    .Where(c => c.Name.Contains(freeTextFilter))
    .OrderBy(c => importantCategories.Contains(c.CategoryId) ? 0 : 1)
    .ThenBy(c => c.Name);
于 2013-02-02T20:44:42.300 に答える