C# Transpose Columns in rows

Asked

Viewed 446 times

-1

In C# - Linq: I have a List of objects:

Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |

Test | 15 | 20 | 45 | 52 |

I need to transpose this line from a collection to an integer list
15
20
45
52

Could someone help?

I need a well optimized version for this, without the load of having to iterate something.

Thank you.


To be clearer I will post the method in which I run my threads, it is precisely the foreach of the tens that I need to avoid:

    public static void DoWork()
    {
        LFAcertos = new List<Lotofacil>();

        var probabilidades = EnumerableExtentions.Combinations(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }, 15);
        int acertos = 0;
        int seqI = 0;
        Int16 idMax = LotofacilService.GetMaxConcurso();

        string info = string.Empty;

        if (idMax < LFConcursos.Count)
        {
            for (Int16 i = idMax; i <= LFConcursos.Count; i++)
            {
                if (!idConEmUso.Contains(i) && !LotofacilService.ExistLotofacil(i))
                {
                    info = string.Format("Lendo Concurso {0} em {1}", i, DateTime.Now);
                    Console.WriteLine(info);
                    log.Info(info);
                    idConEmUso.Add(i);

                    seqI = 0;
                    foreach (var seq in probabilidades)
                    {
                        seqI++;
                        acertos = 0;                            

                        foreach (int dezena in seq)
                        {
                            if (LFConcursos.Any(a => a.IdConcurso == i &&
                                  (a.Bola1 == dezena ||
                                   a.Bola2 == dezena ||
                                   a.Bola3 == dezena ||
                                   a.Bola4 == dezena ||
                                   a.Bola5 == dezena ||
                                   a.Bola6 == dezena ||
                                   a.Bola7 == dezena ||
                                   a.Bola8 == dezena ||
                                   a.Bola9 == dezena ||
                                   a.Bola10 == dezena ||
                                   a.Bola11 == dezena ||
                                   a.Bola12 == dezena ||
                                   a.Bola13 == dezena ||
                                   a.Bola14 == dezena ||
                                   a.Bola15 == dezena
                                )))
                            {
                                acertos++;
                            }
                        }

                        if (acertos >= 14)
                        {
                            //info = string.Format("{0} acertos encontrados para o concurso {1} em {2}",acertos, i,DateTime.Now);
                            //Console.WriteLine(info);
                            //log.Info(info);
                            LotofacilService.SalvaLotofacil(new Lotofacil
                            {
                                IdConcurso = i,
                                IdSeqProb = seqI,
                                Bola1 = seq.Take(1).Single(),
                                Bola2 = seq.Take(2).Skip(1).Single(),
                                Bola3 = seq.Take(3).Skip(2).Single(),
                                Bola4 = seq.Take(4).Skip(3).Single(),
                                Bola5 = seq.Take(5).Skip(4).Single(),
                                Bola6 = seq.Take(6).Skip(5).Single(),
                                Bola7 = seq.Take(7).Skip(6).Single(),
                                Bola8 = seq.Take(8).Skip(7).Single(),
                                Bola9 = seq.Take(9).Skip(8).Single(),
                                Bola10 = seq.Take(10).Skip(9).Single(),
                                Bola11 = seq.Take(11).Skip(10).Single(),
                                Bola12 = seq.Take(12).Skip(11).Single(),
                                Bola13 = seq.Take(13).Skip(12).Single(),
                                Bola14 = seq.Take(14).Skip(13).Single(),
                                Bola15 = seq.Take(15).Skip(14).Single(),
                                Acertos = acertos
                            });
                        }
                    }
                }
                Thread.Sleep(2000);
            }
        }
    }
  • What have you? Show us your code so we can better understand the problem and tell us your specific question.

  • Friend there is no possibility to do this without iterating. Even the most "simple" code possible behind what you can see will make an iteration for sure to get the result you want. In programming, even today there is no magic.

  • What is this "Collecion de List of objects"? It is a List<List<int>>, Dictionary<string, List<int>> or is it something else?

  • Good people, the problem is simple, I have a List<Minhaclasse> where, the class "Minhaclasse" has several attributes, what I need is to take the interim attributes of a given index and turn into a List<int>

  • It would be a movement unlike a PIVOT, that is, a set of columns would be transformed into a column of n rows.

1 answer

0

Using Miniature to change the dozens foreach you can use the COUNT(), something like.

acertos = seq.Count(s => (LFConcursos.Any(a => a.IdConcurso == i &&
                                  (a.Bola1 == s.dezena ||
                                   a.Bola2 == s.dezena ||
                                   a.Bola3 == s.dezena ||
                                   a.Bola4 == s.dezena ||
                                   a.Bola5 == s.dezena ||
                                   a.Bola6 == s.dezena ||
                                   a.Bola7 == s.dezena ||
                                   a.Bola8 == s.dezena ||
                                   a.Bola9 == s.dezena ||
                                   a.Bola10 == s.dezena ||
                                   a.Bola11 == s.dezena ||
                                   a.Bola12 == s.dezena ||
                                   a.Bola13 == s.dezena ||
                                   a.Bola14 == s.dezena ||
                                   a.Bola15 == s.dezena
                                ))))
  • That’s exactly what I want to avoid, I think there must be some way to do this with Linq.

  • Ceeeerto, user o select from Linq, but I don’t think you’ll be very efficient with it. https://dotnetfiddle.net/TDg8aQ

  • I have a set of probabilities that has about 3 million records and each index contains 15 integers, think of the delay that is generating this foreach rs.

Browser other questions tagged

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