对列表进行排序通过匹配的枚举名称值

发布于 2024-11-09 03:04:35 字数 431 浏览 0 评论 0原文

有点不确定如何做到这一点。我需要以与文件服务器上显示的顺序不同的顺序显示文件列表。

我在想一种方法是按匹配的枚举名称值对字符串列表进行排序。

假设我有一个完整的字符串列表:

    List<string> filenames = new List<string>();

并且我有一个相关的枚举以按特定顺序显示文件:

    public enum ProcessWorkFlowOrder 
    {    
       File1,
       File3,
       File2           
    }

列表中的“文件名”字符串值将与枚举的名称完全匹配。

按匹配的枚举值对文件名列表进行匹配和排序的最佳方法是什么?

A little unsure of how to do this. I need to display a list of files in a different order than they are presented on our file server.

I was thinking one way would be to order a list of strings by a matched enum's name value.

Let's say I have a full list of strings:

    List<string> filenames = new List<string>();

And I have a related enum to show the files in a certain order:

    public enum ProcessWorkFlowOrder 
    {    
       File1,
       File3,
       File2           
    }

The "filenames" string value in the List will match exactly the Enum's name.

What is the best way to match and order the FileNames list by its matched enum value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话里做英雄 2024-11-16 03:04:35

如果文件名完全匹配,我不会使用枚举,而是使用另一个保留顺序的列表。

List<string> namesInOrder = ...
List<string> files = ...

var orderedFiles = from name in namesInOrder
                   join file in files 
                   on name equals file
                   select file;

Join 保留第一个序列的顺序,因此允许您使用它来对第二个序列进行排序。

If the filenames match exactly, I wouldn't use an enum but rather another order-preserving list.

List<string> namesInOrder = ...
List<string> files = ...

var orderedFiles = from name in namesInOrder
                   join file in files 
                   on name equals file
                   select file;

Join preserves the order of the first sequence and will therefore allow you to use it to order the second.

墨小墨 2024-11-16 03:04:35

如果您已经有了枚举,并且无法像安东尼建议的那样将其更改为字符串列表,则可以使用以下命令:

var order = Enum.GetValues(typeof(ProcessWorkFlowOrder))
                .OfType<ProcessWorkFlowOrder>()
                .Select(x => new {
                                   name = x.ToString(),
                                   value = (int)x
                        });

var orderedFiles = from o in order
                   join f in filenames
                   on o.name equals f
                   orderby o.value
                   select f;

If you already have the enum and you can't change it to a list of string like Anthony suggests, you can use this:

var order = Enum.GetValues(typeof(ProcessWorkFlowOrder))
                .OfType<ProcessWorkFlowOrder>()
                .Select(x => new {
                                   name = x.ToString(),
                                   value = (int)x
                        });

var orderedFiles = from o in order
                   join f in filenames
                   on o.name equals f
                   orderby o.value
                   select f;
独夜无伴 2024-11-16 03:04:35

怎么样?

var orderedFilenames = filenames.OrderBy(
    f => (int)Enum.Parse(typeof(ProcessWorkFlowOrder), f));

但是,我不确定维护这个枚举是最好的方法,最好编写一个函数来定义顺序算法,如果枚举不包含文件名怎么办。

使用 TryParse 编辑

var orderedFilenames = filenames.OrderBy(f => {
       ProcessWorkFlowOrder parsed;
       int result = 0;
       if (Enum.TryParse<ProcessWorkFlowOrder>(f, out parsed))
           result = (int)parsed;
       return result;
    });

您将使用什么规则来构造枚举或顺序保留列表,或者顺序是否纯粹是任意的?

how about?

var orderedFilenames = filenames.OrderBy(
    f => (int)Enum.Parse(typeof(ProcessWorkFlowOrder), f));

However, I'm not sure that maintaining this enum is the best approach, better to write a function to define the order algorithm, what if enum does not contain the file name.

EDIT with TryParse

var orderedFilenames = filenames.OrderBy(f => {
       ProcessWorkFlowOrder parsed;
       int result = 0;
       if (Enum.TryParse<ProcessWorkFlowOrder>(f, out parsed))
           result = (int)parsed;
       return result;
    });

What rules would you use to construct the enum or an order preserving list or, is the order purely arbitary?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文