【问题标题】:How do i add database values to a dictionary containing a class as object如何将数据库值添加到包含类作为对象的字典中
【发布时间】:2019-10-11 13:15:40
【问题描述】:

我有一个字典,其中包含一个作为 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 字段,现在我想从数据库值“artcode”为 ComponentNr 的数据库中获取“Aantal”和“Omschrijving”

查询:

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

【问题讨论】:

  • 你为什么想要“
    ”?
  • @jdweng 这是一个错字
  • 来自数据库
  • 组件组件 = resultaten["38292000"];组件.Aantal = 16;

标签: c# sql-server xml linq


【解决方案1】:

因此,第 1 步是从 xml 填充 Dictionary&lt;string, Component&gt;。然后第 2 步是运行数据库查询以完成填写存储在Dictionary 中的Component 对象?

ComponentNrDictionary 的键,并且您在"artcode" 字段中的数据库查询中有ComponentNr,因此您只需使用"artcode"Dictionary 中查找值即可修改它。

// 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.
}

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多