简体   繁体   English

根据类对象对类进行分组和排序?

[英]Grouping and ordering class object based on there certain properties?

I have product class which has Description property and have 3 product objects: 我有一个具有Description属性的product类,并具有3 product对象:

Product product1 = new Product("This is bottom product");
Product product2 = new Product("This is top product");
Product product3 = new Product("This is medium product");

List<Product> lst = new List<Product>();
lst.Add(product1);
lst.Add(product2);
lst.Add(product3);

I want to re-arrange these objects so that all products with description of top comes on top and product with description as bottom comes at bottom. 我想重新排列这些对象,以便所有具有top descriptionproducts都位于顶部,具有bottom description product位于底部。

var res = lst.Where(p => p.Desription == "This is top product")
                         .Concat(lst.Where(p => p.Desription == "This is medium product"))
                         .Concat(lst.Where(p => p.Desription == "This is bottom product")).ToList();

I have coded the above query to achieve this. 我已经对上述查询进行了编码以实现此目的。 This returns me correct results. 这给我返回正确的结果。 But I am not sure if this is most efficient way to solve this problem? 但是我不确定这是否是解决此问题的最有效方法?

Can someone suggest any alternative/performance-wise-better approach? 有人可以建议任何替代/性能更好的方法吗?

Note: I have simplified the problem here. 注意:我已经在这里简化了问题。 Otherwise product has many more objects and properties. 否则,产品将具有更多的对象和属性。

Edited: 编辑:

The better solution than comparer(quicker): 比比较器(更快速)更好的解决方案:

 public enum ProductType
    {
        Bottom=0,
        Medium,
        Top,
    }

    public class Product
    {
        public ProductType Type { get; set; }
        //the rest of properties here
    }

 var list = new List<Product>();
 var orderedList = list.OrderBy(x => (int)x.Type).ToList();

If you don't want to add anything to the existing class, maybe wrap it or extend? 如果您不想在现有类中添加任何内容,则可以将其包装或扩展?

Given this in your question: 鉴于您的问题:

var res = lst.Where(p => p.Desription == "This is top product")
                         .Concat(lst.Where(p => p.Desription == "This is medium product"))
                         .Concat(lst.Where(p => p.Desription == "This is bottom product")).ToList();

I believe what you are looking for is something like this: 我相信您正在寻找的是这样的:

var res = lst.OrderBy(p => p.Desription);

I'd like to point out that your variable for "Description" is misspelled. 我想指出的是,您用于“说明”的变量的拼写错误。

Use a helper Dictionary : 使用辅助Dictionary

Dictionary<string, int> myDic = new Dictionary<string, int>
{
    {"top", 1},
    {"medium", 2},
    {"bottom", 3}
};
var res = lst.OrderBy(c => myDic[c.Desription.Split(' ')[2]]);

Try something like this, if you know each description will only contain the specific wording 'top', 'bottom', 'medium' and only one of those: 如果您知道每个描述将仅包含特定的单词“顶部”,“底部”,“中等”,并且仅包含以下其中之一 ,请尝试以下操作:

List<Product> res = new List<Product>();
for (int i = 0; i < lst.Count(); i++)
{
    res.AddRange(lst.Where(p => p.Desription.Contains("top").ToList());
    res.AddRange(lst.Where(p => p.Desription.Contains("medium").ToList());
    res.AddRange(lst.Where(p => p.Desription.Contains("bottom").ToList());
}

I use AddRange instead of Add since there may be multiple 'top', 'medium' or 'bottom' products. 我使用AddRange而不是Add因为可能有多个“顶部”,“中等”或“底部”产品。

Also it won't work if the descriptions contain 'top' and 'bottom' in the same sentence, 'top' and 'medium', etc. 如果描述在同一句子中包含“ top”和“ bottom”,“ top”和“ medium”等,也将不起作用。

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

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