如何将数据库值添加到包含类作为对象的字典中

丹泽尔·奥比斯波

我有一个包含字符串作为 TKey 和一个类“组件”作为 TValue 的字典。我的问题是想要将数据库值和 xml 值添加到类字段中。

这是我的类组件:

public class Component
    {
        public string ComponentNr { get; set; }
        public string Omschrijving { get; set; }
        public int Aantal { get; set; }
        public int Pos { get; set; }
    }

我已经用 xml 属性值填充了 ComponentNr 和 Pos 字段,现在我想从数据库中获取“Aantal”和“Omschrijving”,其中数据库值“artcode”是 ComponentNr

查询:

SqlCommand dgCommand = new SqlCommand("select g.artcode, i.Description, sum(g.aantal*-1) as aantal " +
                                                      "from gbkmut g " +
                                                      "join Items i on g.artcode = i.ItemCode " +
                                                      "where 1=1 and g.project=@projectnr and g.reknr=3000 and g.bud_vers='MRP' and g.operation='VOORSMD' and g.artcode !='H MAN' and i.Description not like 'pcb %' " +
                                                      "group by g.artcode, i.Description, g.aantal ", conn);

这就是我现在用 xml 属性值填充字典的方法:

Dictionary<string, Component> resultaten = componenten;
List<string> files = fileList;
string file;
file = files.Find(x => x.Contains(faberNr));
XDocument xdoc = XDocument.Load(file);
List<Component> components = xdoc.Descendants("Part").Select(x => new Component()
                    {
                        ComponentNr = (string)x.Elements().Where(y => y.Attribute("PartsName") != null)
                                                            .Select(y => (string)y.Attribute("PartsName")).FirstOrDefault(),
                        Pos = (int)x.Descendants().Where(y => y.Attribute("Setno") != null)
                                                    .Select(y => (int)y.Attribute("Setno")).FirstOrDefault()
                    }).ToList();
                    resultaten = components.GroupBy(x => x.ComponentNr, y => y).ToDictionary(x => x.Key, y => y.FirstOrDefault());
                    return resultaten;

示例:
我的预期输出是:

 Key = 38292000  
 Value = Component 
   Aantal = 16  
   ComponentNr = 38292000 
   Omschrijving = Omschrijving123 
   Pos = 12

我的实际输出是:

Key = 38292000 
Value = Component  
 Aantal = 0  
 ComponentNr = 38292000 
 Omschrijving = null  
 Pos = 12
约书亚罗宾逊

因此,第 1 步是Dictionary<string, Component>从 xml填充 a 然后第 2 步是运行数据库查询以完成填写Component存储在Dictionary?

ComponentNr是进入 的键Dictionary,并且您ComponentNr"artcode"字段中的数据库查询中获得,因此您只需从Dictionaryusing查找值"artcode"然后修改它。

// reader is an instance of SqlDataReader you got from the SqlCommand.
// resultaten is the Dictionary<string, Component> that you populated earlier.
// If these columns can be null in the database, don't forget to check them for DBNull.Value.
string componentNr = Convert.ToString(reader["artcode"]);
if (resultaten.TryGetValue(componentNr, out Component value))
{
   value.Aantal = Convert.ToInt32(reader["aantal"]);
   value.Omschrijving = Convert.ToString(reader["Description"]);
}
else
{
   // Component exists in the database but not in the Dictionary.
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将复选框中的值添加到数据库

如何将一些值添加到我的数据库中

如何将新对象添加到现有实体,将数据放入数据中并让EF将其添加到我的数据库中?

如何将 css 类添加到 mysql 数据库的最后 10 个条目?

如何将具有递增ID的对象组添加到数据库?

如何将多个地图对象(JSON)添加到cloud-firestore数据库?

如何将默认值添加到变量并存储在 Django 的数据库中?

如何将列表添加到字典的值中?

如何将HH:MM时间值添加到Access数据库?

如何将新数据从ebay添加到Django中的数据库?

如何将新的数据行添加到我的 Derby 数据库中?

如何将新数据添加到 android 中的 Firebase 数据库?

如何将列表中的数据动态添加到数据库(PostgreSQL)

如何将数据添加到Firestore数据库中的现有文档

如何将数据添加到 firebase 数据库中已有的引用?

如何将多个python变量添加到SQL数据库中?

如何将条形图添加到基于Django的数据库中?

如何将NullableBooleanField添加到现有的Django数据库中?

如何将列表元素添加到熊猫数据库中的行元素

如何将包含对象的数组中的数据动态添加到嵌套数组?

如何将包含数据库中数据的自定义表添加到 Oracle APEX 中的应用程序?

如何将值添加到类对象,然后将其放入数组中?

如何将新值添加到对象中

Java:如何将数据添加到对象中?

如何防止/限制将空白值添加到Java中的mysql数据库?

如何将每个单词或获取字符串作为单独的条目添加到数据库

如何将属性添加到包含条目(DataNucleus)的 Neo4j 数据库表?

如何将字典作为另一个对象添加到文件中的原始嵌套

如何将字典键值作为属性添加到对象