Play list in an array

Asked

Viewed 44 times

3

I created these two variables to be one array

public static string[] doc { get; set; }
public static string[] doc1 { get; set; }

And I have these two items, where is a list

var xDoc = XDocument.Parse(soapResult, LoadOptions.None)
    .Descendants(ns + "ListaNfse")
    .Descendants(ns + "CompNfse")
    .Descendants(ns + "Nfse")
    .Elements(ns + "InfNfse")
    .Select(x => new
    {
        OutrasInformacoes = x.Element(ns + "OutrasInformacoes")?.Value,
        Numero = x.Element(ns + "Numero")?.Value,
        Codigo = x.Element(ns + "CodigoVerificacao")?.Value,
    }).ToList();

var xDoc1 = XDocument.Parse(soapResult, LoadOptions.None)
    .Descendants(ns + "ListaNfse")
    .Descendants(ns + "CompNfse")
    .Descendants(ns + "Nfse")
    .Elements(ns + "InfNfse")
    .Descendants(ns + "DeclaracaoPrestacaoServico")
    .Descendants(ns + "InfDeclaracaoPrestacaoServico")
    .Descendants(ns + "Rps")
    .Elements(ns + "IdentificacaoRps")
    .Select(x => new
    {
        NumeroRPS = x.Element(ns + "Numero")?.Value,
    }).ToList();

How can I play the values of these lists in the array ?

I tried something like:

doc = new string[] { xDoc.ToString() };
doc1 = new string[] { xDoc1.ToString() };

But it didn’t work, how can I play, and then use this array?

1 answer

2


If the results are of the type string can do as follows:

string[] doc = xDoc.Select(r => $"{r.OutrasInformacoes}|{r.Numero}|{r.Codigo}").ToArray();;
string[] doc1 = xDoc1.ToArray();

The array doc keeps the 3 properties concatenated with a separator between them (|).

Then, when you need to use the values of array doc can do the following:

var docTmp = (from arr in array
              select new
              {
                  OutrasInformacoes = arr.Split('|')?[0],
                  Numero = arr.Split('|')?[1],
                  Codigo = arr.Split('|')?[2]
              }).ToList();
  • I tried it this way, and it returned me the following error: It is not possible to convert implicitly type "<Anonymous type: string Outrasinformacoes, string Numero, string Codigo>[]" into "string[]"

  • The xDoc has 3 properties, it is not possible to convert this directly to a Array. You can’t put that information together string? Or which property should go to Array?

  • I need the 3 guys, how can I get these figures ?

  • Edited response.

  • That’s just what I needed, thank you for the answer and the explanation, I understood. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.