简体   繁体   English

Orderby,然后使用LINQ

[英]Orderby and thenby using LINQ

I have below table for categories. 我在下表中列出了类别。

tbCategories tbCategories

CatgID        CatgName
------        --------
  1           Catg1
  2           Catg2
  3           Catg3
  4           Catg4

Now I have items table which is categorized linking to above categories 现在我有项目表,它被分类链接到上述类别

tbItem tbItem

ItemID            ItemName           ItemCatg         Priority
------            --------           --------         --------
  1                Name 1               1                 1
  2                Name 2               1                 2
  3                Name 3               2                 1
  4                Name 4               3                 3
  5                Name 5               4                 2
  6                Name 6               3                 1

Priority 优先

1 - High
2 - Medium
3 - Low

Now when I display the item data, I want the to first display items with category Catg1 , Catg2 and Catg4 based on their priority, then display items with Catg3 based on their priority. 现在,当我显示项目数据时,我希望首先根据其优先级显示类别Catg1Catg2Catg4的项目,然后根据其优先级显示Catg3的项目。

I had tried below LINQ expression, but I did not succeed with that. 我曾尝试在LINQ表达式下进行操作,但没有成功。 My knowledge on LINQ or SQL is not too strong but that's how much I could reach. 我对LINQSQL知识不是很强,但是我可以达到的水平。

model.items = db.tbItem.OrderBy(p=>p.ItemCatg).ThenBy(p=>p.Priority).ToList();

Could someone please show me the right way to achieve this functionality? 有人可以告诉我实现此功能的正确方法吗?

UPDATE UPDATE

I would like to achieve below combination while displaying. 我想在显示时实现以下组合。

ItemID            ItemName           ItemCatg         Priority
------            --------           --------         --------
  1                Name 1               1                 1
  3                Name 3               2                 1
  2                Name 2               1                 2
  5                Name 5               4                 2
  6                Name 6               3                 1
  4                Name 4               3                 3

The following is a trick I've used in the past. 以下是我过去使用的技巧。 You're basically relying on the result of the comparison ordering things first by the non-3 categories (ie, the false equates to 0, the true to 1) then by the priority, then by the category. 您基本上是依靠比较结果的,首先是按非3类别(即false等于0,true等于1),然后是优先级,然后是类别对事物进行排序。

tbItems.OrderBy(p=>p.ItemCatg == 3).ThenBy(p=>p.Priority).ThenBy(p=>p.ItemCatg).ToList();

Here's a .NET Fiddle example. 这是一个.NET Fiddle示例。

Here's the output from the example: 这是示例的输出:

ItemID: 1    ItemName: Name 1    ItemCatg: 1    Priority: 1
ItemID: 3    ItemName: Name 3    ItemCatg: 2    Priority: 1
ItemID: 2    ItemName: Name 2    ItemCatg: 1    Priority: 2
ItemID: 5    ItemName: Name 5    ItemCatg: 4    Priority: 2
ItemID: 6    ItemName: Name 6    ItemCatg: 3    Priority: 1
ItemID: 4    ItemName: Name 4    ItemCatg: 3    Priority: 3

Edit 2: 编辑2:

You can use multiple queries like this: 您可以使用多个查询,如下所示:

var query1 = model.items.Where(x => x.ItemCatg != 3).OrderBy(y => y.Priority);
var query2 = model.items.Where(x => x.ItemCatg == 3).OrderBy(y => y.Priority);
var query = query1.Concat(query2);

Edit: 编辑:

If you want to group your item based on certain criteria, you probably could do something like this with LINQ: 如果要基于某些条件对项目进行分组,则可以使用LINQ进行以下操作:

model.items = db.tbItem.GroupBy(p=>p.ItemCatg)
   .OrderBy(p=>p.Priority).ToList();

This will give you groups based on ItemCatg and among that group, you order it by Priority . 这将为您提供基于ItemCatg组,在该组中,您可以按Priority对其进行订购。

Original: 原版的:

Since your query is quite customized, you may consider to create a "mapping" between your actual column ItemCatg value in tbItem with the priority you want to assign: 由于查询是完全自定义的,因此您可以考虑在tbItem实际列ItemCatg值与要分配的优先级之间创建一个“映射”:

ItemCatg -> Actual priority
1 -> 0
2 -> 1
4 -> 2 //here is the trick
3 -> 3

In that case, you could create list for mapping like this: 在这种情况下,您可以像这样创建映射列表:

List<int> prioMap = new List<int>() { 1, 2, 4, 3 };

And then use it like this: 然后像这样使用它:

model.items = db.tbItem.OrderBy(p=>prioMap.IndexOf(p.ItemCatg))
   .ThenBy(p=>p.Priority).ToList();

Main trick: IndexOf will give you the index of the item ( ItemCatg ) in the list. 主要技巧: IndexOf将为您提供列表中项目( ItemCatg )的index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM