Solved

Linq Int concatenation

Posted on 2014-09-11
7
307 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 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

18 Experts available now in Live!

Get 1:1 Help Now