所以,我想在C#
中制作一份报告。
首先,我使用LINQ
加载数据:
var query = (from c in customer
where c.id.Equals(customer_id)
orderby c.id
select c).FirstOrDefault();
然后我让dictionary
包含数据库中的字段名称和标签。
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("name", "Full Name");
list.Add("address", "Address");
list.Add("phone", "Customer Phone");
list.Add("email", "Customer Email");
基本上我想迭代整个dictionary
并使用key
作为字段名称标识符:
foreach (var pair in list)
{
graphic.DrawString(pair.Value, font, brush, startX, startY + offset);
graphic.DrawString(":", font, brush, semicolonPos, startY + offset);
graphic.DrawString(query.[pair.Key], font, brush, semicolonPos, startY + offset); // This is not working, any suggestion?
offset += 40;
}
所以我的问题是,是否有可能有类似**query.[pair.Key]**
的内容,或者您对此案有任何建议?
我基本上是一名PHP
程序员,所以也许我的思维方式仍然是网络编程:D。对不起,如果我的问题令人困惑。
答案 0 :(得分:1)
foreach (var pair in list)
{
graphic.DrawString(pair.Value, font, brush, startX, startY + offset);
graphic.DrawString(":", font, brush, semicolonPos, startY + offset);
var pi=typeof(Customer).GetProperty(pair.key);
var val= pi.GetValue(query,null);
graphic.DrawString(val, font, brush, semicolonPos, startY + offset);
}
答案 1 :(得分:0)
您可以使用pair.Key而不是查询。[pair.Key]
graphic.DrawString(pair.Key , font, brush, semicolonPos, startY + offset);query.[pair.Key]
答案 2 :(得分:0)
您尝试做的并不是特别好的做法,因为它使用魔术字符串来尝试访问客户对象的属性值。您也没有任何关注点分离 - 视图与数据访问等混合在一起。
您尚未显示“客户”变量的类型,但理想情况下,您将拥有Customer
类型,然后您的“视图”将以强类型方式访问这些属性。如果您描述您正在使用的UI技术,我可以添加一个示例。