Solved

Linq Int concatenation

Posted on 2014-09-11
7
315 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 2017 5 20
Keyboard 2 33
open System.IO.Compression.ZipArchiveEntry as ZipFile without storing to file 2 56
List<PaisEntity> - show some 1 24
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

786 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