?
Solved

Linq Int concatenation

Posted on 2014-09-11
7
Medium Priority
?
339 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

765 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