将多个属性的 CSV 和 concat 后缀拆分为一个列表

我有三个格式为 CSV 的字符串属性:


string Tap70V = "1,2,3,4" //example data

string Tap100V = "10,20,30,40" //example data

string Tap25V = ".2,.4,.6,.8" //example data

我想要列表中的以下结果,以便我可以将它绑定到组合框:


".2 Watt 25V"

".4 Watt 25V"

".6 Watt 25V"

".8 Watt 25V"

"1 Watt 70V"

"2 Watt 70V"

"3 Watt 70V"

等等,直到它显示


"4 Watt 100V"


我知道我需要将这三个属性结合在一起,用“,”将它们拆分成一个列表,然后连接后缀“Watt XXV”。(不一定按这个顺序)


我只是想不出一个好的一两行方法来做到这一点


慕的地8271018
浏览 232回答 4
4回答

Helenr

首先,您应该将变量的值移动到如下所示的字典结构中var tbl = new Dictionary<string, string>() {&nbsp; &nbsp; { "70V", "1,2,3,4" },&nbsp; &nbsp; { "100V", "10,20,30,40" },&nbsp; &nbsp; { "25V", ".2,.4,.6,.8" },};然后您可以使用以下代码轻松查询它tbl.SelectMany(x => x.Value.Split(',').Select(y => $"{y} Watt {x.Key}"));该SelectMany调用展平了多层层次结构,并返回IEnumerable<string>带有所需字符串的 。对于内插字符串的$"{y}"语法,如果您不使用受支持的 C# 版本,则可以使用string.Format而不是字符串内插更新根据 OP 的评论,我提供了一种形成上面字典的方法var tbl = new Dictionary<string, string>() {&nbsp; &nbsp; { nameof(Tap70V), Tap70V },&nbsp; &nbsp; { nameof(Tap100V), Tap100V },&nbsp; &nbsp; { nameof(Tap25V), Tap25V },};依赖变量名来获取值是一种危险的做法。如果 OP 提供了 和 的值如何可用70V,那么我可以用合适的方式更新答案以使其适应该解决方案100V25V

哆啦的时光机

问题是:你怎么知道值的范围与 70V 有关,而值的范围与 25V 有关?但让我们假设您有一系列电压文本组合:Voltage | Text&nbsp; &nbsp;70&nbsp; &nbsp;| "1,2,3,4"&nbsp; 100&nbsp; &nbsp;| "10,20,30,40"&nbsp; &nbsp;25&nbsp; &nbsp;| ".2,.4,.6,.8"例如,从您的三个变量创建它们:IEnumerable<VoltageTextCombination> voltageTextCombinations = new VoltageTextCombination[]{&nbsp; &nbsp; new VoltageTextCombination {Voltage =&nbsp; 70, Text = Tap70V},&nbsp; &nbsp; new VoltageTextCombination {Voltage = 100, Text = Tap100V},&nbsp; &nbsp; new VoltageTextCombination {Voltage =&nbsp; 25, Text = Tap25V},}我为此定义了一个类 VoltageTextCombination。当然,您也可以为此使用匿名类型。要获得您的组合框项目序列,您需要将文本拆分为一系列瓦特,并使用正确的SelectMany 重载来获得一系列电压 - 瓦特组合。之后,您使用 String.Format 将每个电压 - 瓦特组合转换为您想要显示的文本:下面的 LINQ 中使用的常量,用于将输入文本拆分为瓦特,并将电压 - 瓦特组合格式化为字符串:static readonly char[] separatorChars = new char[] {','};const string strFormat ="{0} Watt {1}V";LINQ 查询:var result = voltageTextCombinations.SelectMany(&nbsp; &nbsp; // parameter collectionSelector:&nbsp; &nbsp; // input a source element (= one voltageTextCombination)&nbsp; &nbsp; // output a sequence (= the text of the combination split into Watts&nbsp; &nbsp; combination => combination.Text.Split(separatorChars),&nbsp; &nbsp; // parameter resultSelector: take one source element, and one of every watt&nbsp; &nbsp; // to make a Voltage - Watt combination&nbsp; &nbsp; (voltageTextCombi, splitString) => new&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Voltage = voltageTextCombi.Voltage,&nbsp; &nbsp; &nbsp; &nbsp; Watt = splitString,&nbsp; &nbsp; })&nbsp; &nbsp; // convert every Voltage-Watt combination to one string:&nbsp; &nbsp; .Select(voltageWattCombi => String.Format(strFormat,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; voltageWattCombi.Watt,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; voltageWattCombi.Voltage));非常简单!

元芳怎么了

这是我能想到的最干净的方法来完成我的问题:if (value is IItemLibrarySpeaker s)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<string> taps = s.Speaker25VTaps.Split(',').Select(x => $"{x} Watt/25V").ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taps.AddRange(s.Speaker70VTaps.Split(',').Select(x => $"{x} Watt/70V"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taps.AddRange(s.Speaker100VTaps.Split(',').Select(x => $"{x} Watt/100V"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return taps;&nbsp; &nbsp; &nbsp; &nbsp; }感谢您的建议(即使是与我的问题无关的建议)

喵喵时光机

根据提供的示例数据,我制作了如下示例代码:string Tap70V = "1,2,3,4"; //example data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string Tap100V = "10,20,30,40"; //example data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string Tap25V = ".2,.4,.6,.8"; //example data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var test = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.AddRange(Tap70V.Split(',').Select(val => val + " Watt 70V"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.AddRange(Tap100V.Split(',').Select(val => val + " Watt 100V"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.AddRange(Tap25V.Split(',').Select(val => val + " Watt 25V"));在上面的代码中,将字符串对象值添加到List<string>变量 namedtest中。对于每个对象数据都需要拆分并添加范围到列表中。
打开App,查看更多内容
随时随地看视频慕课网APP