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

C# 将多个属性中的CSV和concat后缀拆分为一个列表,c#,linq,C#,Linq,我有三个格式化为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

我有三个格式化为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瓦100V

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


我只是想不出一个好的一行或两行方法来实现这一点。首先,您应该将变量的值移动到如下的字典结构中

var tbl = new Dictionary<string, string>() {
    { "70V", "1,2,3,4" },
    { "100V", "10,20,30,40" },
    { "25V", ".2,.4,.6,.8" },
};
SelectMany调用将多级层次结构展平,并返回带有所需字符串的IEnumerable。${y}语法如果对于插值字符串,如果不使用受支持的C版本,则可以使用string.Format代替字符串插值

更新

根据OP的评论,我提供了一种形成上述词典的方法

var tbl = new Dictionary<string, string>() {
    { nameof(Tap70V), Tap70V },
    { nameof(Tap100V), Tap100V },
    { nameof(Tap25V), Tap25V },
};

依赖变量名作为值是一种危险的做法。如果OP提供了70V、100V和25V的值是如何可用的,那么我可以用合适的方法更新答案,使其适应此解决方案

if (value is IItemLibrarySpeaker s)
        {
            List<string> taps = s.Speaker25VTaps.Split(',').Select(x => $"{x} Watt/25V").ToList();
            taps.AddRange(s.Speaker70VTaps.Split(',').Select(x => $"{x} Watt/70V"));
            taps.AddRange(s.Speaker100VTaps.Split(',').Select(x => $"{x} Watt/100V"));

            return taps;
        }

感谢您的建议,即使是那些与我的问题无关的建议

根据提供的示例数据,我制作了如下示例代码:

string Tap70V = "1,2,3,4"; //example data
            string Tap100V = "10,20,30,40"; //example data
            string Tap25V = ".2,.4,.6,.8"; //example data

            var test = new List<string>();
                test.AddRange(Tap70V.Split(',').Select(val => val + " Watt 70V"));
                test.AddRange(Tap100V.Split(',').Select(val => val + " Watt 100V"));
                test.AddRange(Tap25V.Split(',').Select(val => val + " Watt 25V"));
在上面的代码段中,将字符串对象值添加到名为test的列表变量中。
对于每个对象,数据都需要拆分并添加到列表中。

问题是:如何知道值的范围与70V有关,而值的范围与25V有关

但我们假设您有一系列电压文本组合:

Voltage | Text
   70   | "1,2,3,4"
  100   | "10,20,30,40"
   25   | ".2,.4,.6,.8"
例如,要从三个变量创建它们:

IEnumerable<VoltageTextCombination> voltageTextCombinations = new VoltageTextCombination[]
{
    new VoltageTextCombination {Voltage =  70, Text = Tap70V},
    new VoltageTextCombination {Voltage = 100, Text = Tap100V},
    new VoltageTextCombination {Voltage =  25, Text = Tap25V},
}
LINQ查询:

var result = voltageTextCombinations.SelectMany(

    // parameter collectionSelector:
    // input a source element (= one voltageTextCombination)
    // output a sequence (= the text of the combination split into Watts
    combination => combination.Text.Split(separatorChars),

    // parameter resultSelector: take one source element, and one of every watt
    // to make a Voltage - Watt combination
    (voltageTextCombi, splitString) => new
    {
        Voltage = voltageTextCombi.Voltage,
        Watt = splitString,
    })

    // convert every Voltage-Watt combination to one string:
    .Select(voltageWattCombi => String.Format(strFormat,
            voltageWattCombi.Watt,
            voltageWattCombi.Voltage));

简单的问候

到目前为止,您尝试了什么?我会让您开始:Tap70V.Split','。Selectx=>${x}瓦特70V。不要使用字符串变量/字段/属性,并尝试像处理数据一样处理它们的名称。变量名就是:变量名。方法的名称只是方法的名称。它们不能理解为程序要处理的一些数据;试图遵循这种方法在概念上是有问题的。相反,也许可以尝试使用字典之类的工具来保存数据,其中整数键是电压,与键相关的值是浮点数列表,每个浮点数都是瓦特数……我不喜欢您尝试的操作。但这里有一个答案:string[]results=string.Join,,newstring[]{Tap70V,Tap100V,Tap25V}.splitnewchar[]{','}.ToArray;创建组合框列表其实并不难。我真正的问题是:当选择一个选项时,您希望将这些值映射到什么。程序如何知道以后如何处理这些值呢?我仍然需要以同样的方式解析这些属性,以便首先生成字典。如果我没有使用数据库中的数据模型,那么这将是采取的合乎逻辑的方法。@beatnikthedan,我假设是一个字典,因为在您当前的模型中,我没有看到任何包含70V、100V、25V等值的变量。如果在某些变量中有这些信息,那么我可以告诉你如何得到相同的结果,在这种情况下,我什么都不需要键,只需要值列表。因此,一开始就把它放在字典里是没有意义的。在我将这些值转换为UI的列表之后,我真的不需要再次引用它。Xaml转换器为我解决了所有这些问题。@beatnikthedan您期望的示例消息是3瓦70V,请注意您使用键的70V部分我并不是说这不起作用,或者以这种方式构造我的特定问题不是一个好主意,它只是不适用于我的应用程序。没有必要把它转换成字典。特别是因为将字典绑定到Xaml控件是非常有问题的。我需要一个IEnumerable,我可以在我必须创建的查询中创建它,甚至可以首先将我的三个属性放入字典中。不需要做这个中间步骤。我不需要知道它是来自25、70还是100个属性。我需要知道我在问题中把它转换成什么值。
static readonly char[] separatorChars = new char[] {','};
const string strFormat ="{0} Watt {1}V";
var result = voltageTextCombinations.SelectMany(

    // parameter collectionSelector:
    // input a source element (= one voltageTextCombination)
    // output a sequence (= the text of the combination split into Watts
    combination => combination.Text.Split(separatorChars),

    // parameter resultSelector: take one source element, and one of every watt
    // to make a Voltage - Watt combination
    (voltageTextCombi, splitString) => new
    {
        Voltage = voltageTextCombi.Voltage,
        Watt = splitString,
    })

    // convert every Voltage-Watt combination to one string:
    .Select(voltageWattCombi => String.Format(strFormat,
            voltageWattCombi.Watt,
            voltageWattCombi.Voltage));