Solved

Linq Int concatenation

Posted on 2014-09-11
7
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

696 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