【发布时间】: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