使用 Linq 创建数组的数组

发布于 2024-10-07 13:15:48 字数 748 浏览 2 评论 0原文

我有以下 XML:

<datarow>
  <datacol><![CDATA[Value1]]></datacol>
  <datacol><![CDATA[Value2]]></datacol>
  <datacol><![CDATA[Value3]]></datacol>
</datarow>
<datarow>
  <datacol><![CDATA[Value5]]></datacol>
  <datacol><![CDATA[Value6]]></datacol>
  <datacol><![CDATA[Value7]]></datacol>
</datarow>
// ...

How can I create an bi-Dimensional array using linq?

我会避免这样做:

foreach("datarow") {
    foreach ("datacol") { ... }
}

谢谢!

[编辑]最终数组应该是这样的:

array[,] = {{ "Value1", "Value2", "Value3"} , { "Value4", "Value5", "Value6"}}

I've the following XML :

<datarow>
  <datacol><![CDATA[Value1]]></datacol>
  <datacol><![CDATA[Value2]]></datacol>
  <datacol><![CDATA[Value3]]></datacol>
</datarow>
<datarow>
  <datacol><![CDATA[Value5]]></datacol>
  <datacol><![CDATA[Value6]]></datacol>
  <datacol><![CDATA[Value7]]></datacol>
</datarow>
// ...

How can I create an bi-dimensional array using linq?

I'll avoid doing :

foreach("datarow") {
    foreach ("datacol") { ... }
}

Thanks !

[EDIT] Final array should be like this:

array[,] = {{ "Value1", "Value2", "Value3"} , { "Value4", "Value5", "Value6"}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一片旧的回忆 2024-10-14 13:15:48

以下是从 XML 数据创建交错数组的示例。

var xmlStr = "<table><dataRow><dataCol>1</dataCol><dataCol>2</dataCol></dataRow><dataRow><dataCol>5</dataCol><dataCol>6</dataCol></dataRow></table>";
var rows = from r in XElement.Parse(xmlStr).Elements("dataRow") select r;

int[][] intJagArray = (from r in rows select (from c in r.Elements("dataCol") select Int32.Parse(c.Value)).ToArray()).ToArray();

这是帮助我解决这个问题的页面。 http://www.daniweb.com/code/snippet267931.html

Here is an example to create a jagged array from XML data.

var xmlStr = "<table><dataRow><dataCol>1</dataCol><dataCol>2</dataCol></dataRow><dataRow><dataCol>5</dataCol><dataCol>6</dataCol></dataRow></table>";
var rows = from r in XElement.Parse(xmlStr).Elements("dataRow") select r;

int[][] intJagArray = (from r in rows select (from c in r.Elements("dataCol") select Int32.Parse(c.Value)).ToArray()).ToArray();

Here's the page that helped me figure this out. http://www.daniweb.com/code/snippet267931.html

恋竹姑娘 2024-10-14 13:15:48

LINQ 和多维数组不能很好地混合。

您可以使用传统的 foreach 循环,但必须首先计算多维数组的大小:

string[,] result = new string
[
    doc.Elements("datarow").Count(),
    doc.Elements("datarow").Max(d => d.Elements("datacol").Count())
];

int x = 0;
foreach (var datarow in doc.Elements("datarow"))
{
    int y = 0;
    foreach (var datacol in datarow.Elements("datacol"))
    {
        result[x, y] = (string)datacol;
        y++;
    }
    x++;
}

但创建交错数组(即一维数组的一维数组)确实要简单得多维数组;LINQ 和一维数组很好地混合!):

string[][] result = doc.Elements("datarow")
                       .Select(d => d.Elements("datacol")
                                     .Select(c => (string)c)
                                     .ToArray())
                       .ToArray();

LINQ and multi-dimensional arrays do not mix well.

You can use a traditional foreach loop, but you have to calculate the size of the multi-dimensional array first:

string[,] result = new string
[
    doc.Elements("datarow").Count(),
    doc.Elements("datarow").Max(d => d.Elements("datacol").Count())
];

int x = 0;
foreach (var datarow in doc.Elements("datarow"))
{
    int y = 0;
    foreach (var datacol in datarow.Elements("datacol"))
    {
        result[x, y] = (string)datacol;
        y++;
    }
    x++;
}

But it's really much simpler to create a jagged array (i.e. a one-dimensional array of one-dimensional arrays; LINQ and one-dimensional arrays mix well!):

string[][] result = doc.Elements("datarow")
                       .Select(d => d.Elements("datacol")
                                     .Select(c => (string)c)
                                     .ToArray())
                       .ToArray();
虐人心 2024-10-14 13:15:48

以下是创建交错数组的代码:

        XDocument doc = XDocument.Load("...");

        string[][] data = (from row in doc.Elements("datarow")
                           select row.Elements("datacol").Select(col => col.Value).ToArray()).ToArray();

我不确定如何使用Linq创建多维数组

Here is code to create Jagged Array:

        XDocument doc = XDocument.Load("...");

        string[][] data = (from row in doc.Elements("datarow")
                           select row.Elements("datacol").Select(col => col.Value).ToArray()).ToArray();

I am not sure how you can create a Multi-Dimensional Array using Linq.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文