【发布时间】:2021-11-19 10:29:43
【问题描述】:
我正在循环通过 List,并尝试将其中一个属性实例化为字符串,但它返回类型:
{Namespace.Collection}
如果我设置一个断点,我可以看到它拥有我需要的值。
如何让它返回值而不是类型?
foreach (var PropertyName in ListName) {
string n = PropertyName.ToString();
}
更新(添加了更多我的代码,以及尝试实施建议的解决方案):
foreach (DataRow dr in ds.Tables[0].Rows) {
//PaidTrips is my ObservableCollection instance.
//PaidTrip is my holder class which it has been bound to.
PaidTrips.Add(new PaidTrip {
LicenseHolderID = dr[0].ToString(),
// adding more properties
});
List<PaidTrip> theseTrips = PaidTrips
.GroupBy(p => new { p.LicenseHolderID })
.Select(g => g.First())
.ToList();
foreach (PaidTrip PaidTrips in theseTrips) {
foreach (var LicenseHolderID in PaidTrips.GetType().GetProperties()) {
string n = LicenseHolderID.GetValue(PaidTrips).ToString();
// code to create PDF
}
gfx.DrawString(n, new XFont("Arial", 40, XFontStyle.Bold), ridelGreen, new XPoint(40, 350));
这就是我对string n 所做的事情。但是创建PDF时,字符串输出是System.Action1[System.Action]`
我做错了什么?
【问题讨论】:
-
ListName的类型是什么,怎么定义的? -
string n = PropertyName.GetType().ToString();在这种情况下n的值是多少? -
@TheGeneral 列表是这样的:
List<ClassName> listName = ListName .GroupBy(p => new { p.PropertyName}) .Select(g => g.First() .ToList();其中ListName是ObservableCollection -
@mjwills
n的值仍然是{Namespace.ListName}。 “ListName”是ObservableCollection的名称,我基于List。
标签: c# string list types foreach