使用Linq的通用列表中的值格式化字符串[英] Formatting a string using values from a generic list by LINQ

本文是小编为大家收集整理的关于使用Linq的通用列表中的值格式化字符串的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

问题更新

我有一个通用列表,其中可以包含以下值:

Sector 1
Sector 2
Sector 4

或以下值:

All Sectors

我想在这样的字符串中格式化此值:

Sector 1 & 2 & 4 - 

All Sectors -

当前,我有以下代码以将其格式化相同.它有效,但非常复杂.

string retrieveSectors += sectors.Count == 0
                               ? string.Empty
                               : sectors.OrderBy(
                               y =>
                               y.Sector.Substring(
                               y.Sector.Length - 1, 1)).
                               GroupBy(g => g.Sector).Select(
                               g => g.First()).ToList().Aggregate(
                               retrieveSectors,
                               (current, y) =>
                               (current == retrieveSectors
                               ? current +
                               y.Sector
                               : current + " & " +
                               y.Sector.
                               Substring(
                               y.Sector.
                               Length - 1, 1))) + " - "

在上述代码中,变量扇区是通用列表.有人可以帮助我以简化的方式获得结果.或可以修改上述代码,以使其更容易理解.

任何帮助.谢谢

推荐答案

也许更简单:

    string retrieveSectors =
        string.Format(
        "sectors {0} -",
        sectors.Select(s => s.Replace("sector ", "").Replace("|", ""))
            .OrderBy(s => s)
            .Aggregate((a, b) => string.Format("{0} & {1}", a, b))
        );

其他推荐答案

尝试一下!

List<String> list = new List<String>() { "Sector 1", "Sector 2", "Sector 4" };

(list.Count == 0 ? "Not any sector " :    
((list.Contains("All Sectors") ? "All Sectors " :
    "Sector " + String.Join(" & ", list.OrderBy(c => c).ToArray())
        .Replace("Sector", String.Empty)))) + " - "

也适用于:

List<String> list = new List<String>();
List<String> list = new List<String>() { "All Sectors" };

其他推荐答案

假设source是存储扇区的任何形式的IEnumerable<string>,可能更简洁的语法是:

String.Join(" & ", source).Replace(" Sector ", " ")

或此,如果source可能是无序的,并且您需要订购的扇区号码:

String.Join(" & ", source.OrderBy(s => s)).Replace(" Sector ", " ")

最后,像雷纳托(Renato

source.Any() ? String.Join(" & ", source.OrderBy(s => s)).Replace(" Sector ", " ") : "No sectors"

在您首先提到的两种情况下,所有这些解决方案无论如何都可以使用(简而言之,后者是增强的版本来处理可能发生合理发生并感兴趣的附加案例).

本文地址:https://www.itbaoku.cn/post/1556783.html

问题描述

Question Updated

I have a generic list which can contains the following values:

Sector 1
Sector 2
Sector 4

or the following value:

All Sectors

I want to format this values in a string like this:

Sector 1 & 2 & 4 - 

or

All Sectors -

Currently, I have the following code to format the same. It works, but is very complicated.

string retrieveSectors += sectors.Count == 0
                               ? string.Empty
                               : sectors.OrderBy(
                               y =>
                               y.Sector.Substring(
                               y.Sector.Length - 1, 1)).
                               GroupBy(g => g.Sector).Select(
                               g => g.First()).ToList().Aggregate(
                               retrieveSectors,
                               (current, y) =>
                               (current == retrieveSectors
                               ? current +
                               y.Sector
                               : current + " & " +
                               y.Sector.
                               Substring(
                               y.Sector.
                               Length - 1, 1))) + " - "

In the above code, the variable sector is the generic list. Can someone help me to attain the results in a simplified way.? Or may be modify the above code so that it is more understandable.

Any help appreciated. Thanks

推荐答案

Maybe a little more simple:

    string retrieveSectors =
        string.Format(
        "sectors {0} -",
        sectors.Select(s => s.Replace("sector ", "").Replace("|", ""))
            .OrderBy(s => s)
            .Aggregate((a, b) => string.Format("{0} & {1}", a, b))
        );

其他推荐答案

Try this out!

List<String> list = new List<String>() { "Sector 1", "Sector 2", "Sector 4" };

(list.Count == 0 ? "Not any sector " :    
((list.Contains("All Sectors") ? "All Sectors " :
    "Sector " + String.Join(" & ", list.OrderBy(c => c).ToArray())
        .Replace("Sector", String.Empty)))) + " - "

Also work for:

List<String> list = new List<String>();
List<String> list = new List<String>() { "All Sectors" };

其他推荐答案

Assuming source is any kind of IEnumerable<string> where Sectors are stored, probably the more concise syntax is :

String.Join(" & ", source).Replace(" Sector ", " ")

Or this, if source might be unordered and you want ordered sector numbers :

String.Join(" & ", source.OrderBy(s => s)).Replace(" Sector ", " ")

And finally, ultimate refinement to check for "no sector at all" like in Renato's answer :

source.Any() ? String.Join(" & ", source.OrderBy(s => s)).Replace(" Sector ", " ") : "No sectors"

All of these solutions work anyway in the 2 cases you mentioned at first (simply, the 2 latter are enhanced versions to deal with addtional cases which might reasonably occur and interest you).