使用linq将字符串转换为3D数组

时间:2016-11-22 21:03:38

标签: c# linq

我看过示例,并实现了linq将分隔字符串转换为2D数组,如下所示:

using System;
using System.Linq;

namespace AACOBusinessModel.Extensions
{
  [Serializable]
  public class TwoDimensionalStringArray
  {
    public TwoDimensionalStringArray(string data)
    {
      // 1;2;3^^4;5;6
      this.Data = (
          from string line
          in data.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
          select line.Split(';')
        ).ToArray();
    }

    public TwoDimensionalStringArray() { }

    public readonly string[][] Data = { };

    public override string ToString()
    {
      return string.Join("^^",
        from string[] line
        in Data
        select string.Join(";", line)
      );
    }
  }
}

当我尝试用3D工作时,我遇到了一个问题:

using System;
using System.Linq;

namespace AACOBusinessModel.Extensions
{
  [Serializable]
  public class ThreeDimensionalStringArray
  {
    public ThreeDimensionalStringArray(string data)
    {
      // 1;2;3^^4;5;6@4;4;4^^7;7;7
      this.Data = (
          from string line2D
          in data.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
          from string line
          in line2D.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
          select line.Split(';')
        );
    }

    public ThreeDimensionalStringArray() { }

    public readonly string[][][] Data = { };

    public override string ToString()
    {
      return string.Join("@",
        from string[][] line2D
        in Data
        select string.Join("^^", 
          from string[] line 
          in line2D
          select string.Join(";", line)
        )
      );
    }
  }
}

据我所知,对于2D,结束选择是转换为数组的分裂集合,从而形成2D数组。

但是对于3D,结束选择需要选择2D数组的集合。我不知道该怎么做。

更新

我提出了这个问题,但如果可能的话,我会想要查询语法。

public ThreeDimensionalStringArray(string data)
{
  this.Data = (
      from string line2D
      in data.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
      select (
        from string line
        in line2D.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
        select line.Split(';')
      ).ToArray()
    ).ToArray();
}

3 个答案:

答案 0 :(得分:2)

不确定如何在查询语法中完成此操作,但这里是方法语法版本:

this.Data = data
    .Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
    .Select(table => table.Split(new[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
        .Select(row => row.Split(';'))
        .ToArray())
    .ToArray();

在查询synax中:

this.Data = (
    from table in data.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
    select
    (
        from row in table.Split(new[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
        select row.Split(';')
    ).ToArray()
).ToArray();

答案 1 :(得分:1)

您需要一个嵌套的select子句来返回嵌套集合。

制作列表然后转换为数组对您有用

this.Data = (
    from string line2D
    in data.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)
    select new List<string>(    
      from string line
      in line2D.Split(new string[] { "^^" }, StringSplitOptions.RemoveEmptyEntries)
      select line.Split(';')).ToArray());

答案 2 :(得分:1)

我通常最终会在.NET项目中使用相同的常见常规扩展:

public static string[] splitR(this string str, params string[] separators) {
    return str.Split(separators, StringSplitOptions.RemoveEmptyEntries); }

public static O[] convert<I, O>(this I[] array, Converter<I, O> converter) {
    return Array.ConvertAll(array, converter); }

public static string joinT<T>(this T[] values, string separator) {
    return string.Join(separator, values); }

然后只是:

string s = "1;2;3^^4;5;6@4;4;4^^7;7;7";

string[][][] s3 = s.splitR("@").convert(x => x.splitR("^^").convert(y => y.splitR(";")));

string s0 = s3.convert(x => x.convert(y => y.joinT(";")).joinT("^^")).joinT("@");