Entwickler-Ecke

Basistechnologien - Hinzufügen Daten d. DB in Dictionary mit zwei Schlüsseln


Chocoemilio - Mo 26.02.18 11:48
Titel: Hinzufügen Daten d. DB in Dictionary mit zwei Schlüsseln
Hallo

Ich habe volgendes Problem, aus einen connectionString werden Daten gelesen, die dann in eine Dictionary hinzugefügt werden. Das Problem ist das der Key zwei mal vorkommen kann.
mit verschiedenen values, zur Zeit wird der Key nur ein mal gelessen, deswegen fehlen Daten.
Die jetzige Abfrage:


C#-Quelltext
1:
2:
3:
4:
5:
6:
static Dictionary<String, Tuple<String, String>> articleInfo = new Dictionary<String, Tuple<String, String>>();

if (!wdpGroup.ContainsKey(group))
{
    wdpGroup.Add(group, new Tuple<stringdecimal>(article, amount));
}


Ich habe mir gedacht das ich anstatt die Tuple eine andere Dictonary einsetze aber ich bekomme nur eine Fehlermeldung


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
static Dictionary<String, Dictionary<String, decimal>> wdpGroup = new Dictionary<String, Dictionary<String, decimal>>();
        public Dictionary<String, Dictionary<String, decimal>> getGroupMappping() 
        {
            //Dictionary<String, Tuple<String, String, String>> articleInfo = new Dictionary<String, Tuple<String, String, String>>();
            String connectionString = "Data Source=mention.wdp.intern; Initial Catalog=mention_wdp; Persist Security Info=True;User ID=********;Password=********";
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                String sql = @"SELECT AEL.ARANUMMER as [group], 
                            (SELECT TOP 1 XUMB.ARANUMMER FROM AEL AS XUMB WITH(NOLOCK) WHERE XUMB.ARIDNR = AEL.ARUMBIDNR) as 
                            group_article,
                            AELX.ARANUMMER as article, 
                            CAST(AEL2.AGMENGE AS NUMERIC(8, 0)) as amount
                            FROM AEL
                            LEFT JOIN AEL2 WITH(NOLOCK)ON ael2.agaidnr = ael.aridnr LEFT JOIN AEL AS AELx WITH(NOLOCK) ON AELx.aridnr = 
                            ael2.agidnr
                            WHERE AEL.ARMANKEY = 1000 AND ael.artyp = 'A' AND ael.argruppe IN('G', 'Z')
                            ORDER BY[group]"
;
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.CommandTimeout = 300;
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    int count = 0;
                    while (reader.Read())
                    {
                        //String group_article = "null";
                        String group = reader.GetString(0).Trim();
                        //if(!reader.IsDBNull(1))
                        //{
                        //    group_article = reader.GetString(1).Trim();
                        //}
                        String article = reader.GetString(2).Trim();
                        decimal amount = reader.GetDecimal(3);

                        if (!wdpGroup.ContainsKey(group))
                        {
                            wdpGroup.Add(group, new Dictionary<stringdecimal>(article, amount)); //Fehlermeldung: Konvertierung nicht möglich
                        }
                    }
            }
            return wdpGroup;
        }


Wäre sehr dankbar über Vorschläge ???

Moderiert von user profile iconChristian S.: Username und Passwort im Connection-String durch Sternchen ersetzt
Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Rechtschreibfehler im Titel geändert
Moderiert von user profile iconTh69: Topic aus C# - Die Sprache verschoben am Mo 26.02.2018 um 12:09


Th69 - Mo 26.02.18 13:29

Hallo und :welcome:

es gibt keinen Dictionary-Konstruktor, der gleich 1 Wert hinzufügt. Dies mußt du separat vorher machen:

C#-Quelltext
1:
2:
3:
4:
var dict = new Dictionary<stringdecimal>();
dict.Add(article, amount);

wdpGroup.Add(group, dict);


Christian S. - Mo 26.02.18 16:04

Sowas hier geht:

C#-Quelltext
1:
new Dictionary<stringdecimal>() {{article, amount}}                    


Chocoemilio - Di 27.02.18 10:15

Cool danke ich habe das jetzt so gelöst:


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Dictionary<stringdecimal> articlesDict;

if (!wdpGroup.ContainsKey(group))
{
    articlesDict = new Dictionary<stringdecimal>();
    wdpGroup.Add(group, articlesDict);
}
else
{
    articlesDict = wdpGroup[group];                            
}
articlesDict.Add(article, amount);


Moderiert von user profile iconChristian S.: Beiträge zusammengefasst


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
//Neue Klasse
        public string item1 { get; set; }
        public string item2 { get; set; }
        public string item3 { get; set; }
        public Int32 item4 { get; set; }


        public DiractoryConvert_to_List(Dictionary<stringdecimal> value1, Dictionary<stringdecimal> value2, string key, Dictionary<stringdecimal> value3)
        {
            this.item2 = value1.ToString();
            this.item1 = value2.ToString();
            this.item3 = key;
            this.item4 = value3;
        }

//Aufruf der Klasse
List<DiractoryConvert_to_List> artikelNummer1 = new List<DiractoryConvert_to_List>();
            artikelNummer1 = (from t in wdpGroup.ToList()
                                  
                              select new DiractoryConvert_to_List
                              (
                                  t.Key,
                                  t.Value
                               )).ToList();


Ein Problem habe ich noch wie kann ich auf die Daten zugreifen, so wie ich das habe funktioniert es nicht ???


Th69 - Di 27.02.18 11:30

Mir scheint es, als ob du dich mit den ganzen Dictionary<> ein wenig verzettelt hast (dein unterer Code ergibt irgendwie überhaupt keinen Sinn)!?


Chocoemilio - Mi 28.02.18 10:24

Hi danke für die Antwort.

Das Problem war das das value ein Index hatte also z.b [0]: {[(key)300023,(value)2]}, der value kann halt bis zu zwei Index haben.
Ich konnte halt nicht darauf auf die einzelnen werte zugreifen. Ich habe das jetzt so gelöst aber ich suche noch eine bessere variante ???


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
class DiractoryConvert_to_List
    {
        public string item1 { get; set; }
        public string item2 { get; set; }
        public string item3 { get; set; }
        public Int32 item4 { get; set; }
        public string item5 { get; set; }
        public Int32 item6 { get; set; }


        public DiractoryConvert_to_List(List<DiractoryConvert_to_List> artikelNummer, string key, Dictionary<stringdecimal> value)
        {
            //Gruppen Key
            this.item1 = '8' + key.ToString().Substring(1);
            
            foreach (var i in value)
            {
                string element = i.ToString();

                string w = element.Split('[')[1];
                string a = "";

                foreach (char chr in w)
                {
                    if(Convert.ToInt32(chr) >= 48 && Convert.ToInt32(chr) <= 57)
                    {
                        a += chr;
                    }
                    else
                    {
                        break;
                    }
                }

                string e = element.Split(',')[1];
                string x = "";

                foreach (char chr in e.Trim())
                {
                    if (Convert.ToInt32(chr) >= 48 && Convert.ToInt32(chr) <= 57)
                    {
                        x += chr;
                    }
                    else
                    {
                        break;
                    }
                }

                //Artikelnummer
                string b_artikel = '8' + a.ToString().Substring(1) + "  ";
                this.item2 += b_artikel;
                //Menge der Gruppe
                this.item3 += x + "  ";

                foreach(DiractoryConvert_to_List p in artikelNummer)
                {
                    if(p.item2.Equals(b_artikel.Trim()))
                    {
                        if (p.item1 != null)
                        {
                            //Lagerort der Artikel in den Gruppen
                            this.item5 += p.item1 + "  ";
                        }
                        else
                        {
                            //Anzahl der fehlenden GruppenLagerorte
                            item6++;
                        }
                    }
                }
            }
        }
    }