Solved

Linq Int concatenation

Posted on 2014-09-11
7
311 Views
Last Modified: 2016-02-15
In below piece of code the problem is in this row: (Par_TabNumOrd.Contains((Int32) (c.TabellaOrdine.ToString() + c.NumeroOrdine.ToString())))
Parameter Par_TabNumOrd contains a list of unions between two integer information: TabellaOrdine and NumeroOrdine. In the linq query there are two corresponding fields of entity dbProduzContext.vi_Carichi. I've to concatenate fields but my solution is not correct. What can I do?

public List<DatiCaricoRpt> GetDettaglioColliDaProdurreDelCarico(Int32 Par_IDPianoCarico, string Par_Lista, List<Int32> Par_TabNumOrd)
            {
                using (var dbProduzContext = new ProduzModel.ProduzContext())
                {
                    // Uso un join di tipo "left join" fra Carichi.CodiceArticolo e AnagArt.CodiceArticolo.
                    var results = from c in dbProduzContext.vi_Carichi
                                  join a in dbProduzContext.vi_AnagArt on c.CodiceArticolo equals a.CodiceArticolo
                                  into a1
                                  from a2 in a1.DefaultIfEmpty()
                                  join ps in dbProduzContext.Carichi_PadriStazioni on c.NumeroColli equals ps.IDCollo
                                  join sa in dbProduzContext.AnaStati_Avanzamento on ps.IDStatoAvanzamento equals sa.IDStatoAvanzamento
                                  where (c.Lista == Par_Lista) &&
                                        (ps.IDPianoCarico == Par_IDPianoCarico) &&
                                        (ps.PgrCollo == 1) &&
                                        (ps.IDLinea == Pubblici.pvclParametri.IDLinea) &&
                                        (ps.IDStazione == Pubblici.pvclParametri.CodStazione) &&
                                        (Par_TabNumOrd.Contains((Int32) (c.TabellaOrdine.ToString() + c.NumeroOrdine.ToString()))) &&
                                        ((ps.IDStatoAvanzamento == (Int16)Class_Costants.pvStatoAvanzamentoCollo.DaProdurre) |
                                         (ps.IDStatoAvanzamento == (Int16)Class_Costants.pvStatoAvanzamentoCollo.Ritardatario) |
                                         (ps.IDStatoAvanzamento == (Int16)Class_Costants.pvStatoAvanzamentoCollo.Escluso))
                                  orderby c.Consegna ascending, c.UnioneColli ascending
                                  select new DatiCaricoRpt
                                  {
                                      Consegna = (Int16)c.Consegna,
                                      TabellaOrdine = (Int16)c.TabellaOrdine,
                                      NumeroOrdine = (Int32)c.NumeroOrdine, 
                                      IDStatoAvanzamento = ps.IDStatoAvanzamento,
                                      IDStatoAvanzamentoD = sa.Descrizione,
                                      NumeroColli = (Int16)c.NumeroColli,
                                      UnioneColli = (Int16)c.UnioneColli,
                                      CodiceArticolo = c.CodiceArticolo,
                                      Qta = (float)c.Quantita,
                                      DescrizioneArticolo = a2.DescrizArticolo,
                                      DescrizAggiuntiva1 = a2.DescrizAggiuntiva1
                                  };

                    return results.ToList();
                }

Open in new window

0
Comment
Question by:fantamen
  • 3
  • 2
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40318608
you are concatenating TabellaOrdine and NumeroOrdine as string, not sure why you are doing that, but surely trying to cast that to Int32 is incorrect and probably unnecessary.
Could you give real examples of what you are trying to achieve with that line?
0
 

Author Comment

by:fantamen
ID: 40318658
Hi Jaime
I give you an example: parameter Par_TabNumOrd has a list of values values (101044765, 101006712, 102003499). First three positions of each value express TabellaOrdine (101) and the others express NumeroOrdine (44765)

In query linq I've to filter entity dbProduzContext.vi_Carichi by the parameter list. dbProduzContext.vi_Carichi has fields TabellaOrdine (int16) and NumeroOrdine (int32).


Thank you
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 40319217
c.NumeroOrdine.ToString()  won't add leading zeros to the string, so you won't get a match when the value is shorter than 6 digits, e.g. 44765
Adding a format code - c.NumeroOrdine.ToString("D6") - should fix that in your current code.

An alternative approach is to use String.Format:
 String.Format({"{0:D3} {1:D6}", c.TabellaOrdine, c.NumeroOrdine) will produce the concatenation you want
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:fantamen
ID: 40319438
Hi Mike

I've take another way to solve my problem because of the urgency to complete the procedure.

Anyway I've tried you suggestions, this is the result:
1. c.NumeroOrdine.ToString("D6") ... return the error no overload for method "string" takes 1 arguments
2. String.Format({"{0:D3} {1:D6}", c.TabellaOrdine, c.NumeroOrdine) ... return the errore string.format is not recognize in linq ....


By
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 40322562
Hi,
I don't understand the first error - ToString() on an integer does take a parameter. You can see this in the Immediate  window:
42.ToString("D6")
"000042"
Format() is a standard static method of String - linq would only see the string result.
One thing, (int) can't be used to convert a string to an integer, so that would have caused a problem, need to use int.Parse(mystring)
Lastly, if the source fields are both numeric types, the easiest and most efficient way to get the combined key is via arithmetic, from the immediate window again:
101 * 1000000 + 6712
101006712  

Glad you solved your problem :-)
Mike
0
 

Author Closing Comment

by:fantamen
ID: 40333496
Thank you for the calculation suggestion to obtain formatted string.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now