Formatting a string using values from a generic list by LINQ

自闭症网瘾萝莉.ら 提交于 2020-01-04 09:28:28

问题


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


回答1:


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))
        );



回答2:


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" };



回答3:


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).



来源:https://stackoverflow.com/questions/5409890/formatting-a-string-using-values-from-a-generic-list-by-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!