简体   繁体   English

LINQ按数字顺序排序

[英]LINQ order by a number sequence

I have a list of products with their category ID, like: 我有一个带有类别ID的产品列表,例如:

ID      CategoryID     Product Name
1       1              Product 1
2       1              Product 2
3       7              Product 3
4       8              Product 4
5       9              Product 5
6       10             Product 6

I want to take this list and order by a list of categoryID, like: 1, 8, 9 and the rest, so I get: 我想通过categoryID列表获取此列表和顺序,例如:1,8,9和其余的,所以我得到:

ID     CategoryID     Product Name
1      1              Product 1
2      1              Product 2
4      8              Product 4
5      9              Product 5
3      7              Product 3
6      10             Product 6

There is any way to this with linq? linq有什么办法吗? Thanks 谢谢

If your category IDs are in a list, you can order like that: 如果您的类别ID在列表中,您可以这样订购:

var list = new List<int>() { 1, 8, 9, 7, 10, ... };

var productsOrdered = from p in products
    let index = list.IndexOf(p.CategoryID)
    order by (index < 0 ? int.MaxValue : index) // in case it is not in the list
    select p;

This query will work only with Linq to Objects, so you need to bring all data unordered from your database. 此查询仅适用于Linq to Objects,因此您需要从数据库中取消所有数据。

Assuming that the 1,8,9 is in a list, we'll call orderList , then while we can keep looking for the position in the list each time, we'll be faster to create a dictionary to look it up quickly. 假设1,8,9在列表中,我们将调用orderList ,然后我们每次都可以继续查找列表中的位置,我们会更快地创建一个字典来快速查找它。

var orderDict = orderList.Select((o, index) => new {ID = o, Order=index}).ToDictionary(oi => oi.ID, oi => oi.Order);
int orderHolder;
var orderedProducts = products.OrderBy(p => orderDict.TryGetValue(p.CategoryID, out orderHolder) ? orderHolder : int.MaxValue);

We don't strictly need to set-up orderDict first, but it makes the logic simpler than scanning through the list every time, and also quicker: O(n + m) rather than O(nm). 我们不需要首先设置orderDict ,但它使逻辑比每次扫描列表更简单,也更快:O(n + m)而不是O(nm)。

You can use Enumerable.OrderBy : 您可以使用Enumerable.OrderBy

var catIDs = new[] { 1, 8, 9 };
var ordered = products
    .OrderByDescending(p => catIDs.Contains(p.CategoryID))
    .ThenBy(p => p.CategoryID);

Edit: Here's a demo: http://ideone.com/O462C 编辑:这是一个演示: http//ideone.com/O462C

var query = from p in productsList
            orderby p.CategoryID descending
            select new {ID = p.ID, CID = p.CategoryID, PName = p.ProductName};

query now contains the products list inordered sequence. query现在包含无序序列的产品列表。 You can enumerate through it like: 您可以通过它枚举如下:

foreach(Product prod in query)
   Console.WriteLine(prod.CID);

Edit: Misunderstood the answer. 编辑:误解了答案。 Will update the answer. 将更新答案。

If you know everything that you want to sort at the top of the list, try this: 如果您知道要在列表顶部排序的所有内容,请尝试以下操作:

var products = new List<Product>();

products.Add(new Product { ID = 1, CategoryID = 1, ProductName = "1" });
products.Add(new Product { ID = 2, CategoryID = 1, ProductName = "2" });
products.Add(new Product { ID = 3, CategoryID = 7, ProductName = "3" });
products.Add(new Product { ID = 4, CategoryID = 8, ProductName = "4" });
products.Add(new Product { ID = 5, CategoryID = 9, ProductName = "5" });
products.Add(new Product { ID = 6, CategoryID = 10, ProductName = "6" });

products
    .OrderByDescending(p => p.CategoryID == 1 || p.CategoryID == 8 || p.CategoryID == 9)
    .ThenBy(p => p.CategoryID);

Produces this (from LinqPad): 产生这个(来自LinqPad):

ID CategoryID ProductName 
1  1          1 
2  1          2 
4  8          4 
5  9          5 
3  7          3 
6  10         6 

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

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