我有对象列表:
List<dynamic> prods = new List<dynamic>();
foreach (DataRow row in dataTable.Rows)
{
dynamic obj = new ExpandoObject();
IDictionary<string, object> oprops = obj;
foreach (System.Data.DataColumn col in dataTable.Columns)
oprops.Add(col.ColumnName, row[col.ColumnName]);
obj = oprops;
list.Add(obj);
}
pricing
列表的填写方式与产品DataTable
定价相同。
我有这个查询:
var result = (from product in prods join price in pricing on product.ID equals price.ProductID select new { product, price }).ToList();
我想在结果集中添加更多属性。
foreach (dynamic obj in q)
{
obj.Test = "yes, testing."; // This line gives runtime error
Console.WriteLine(
"{0} - {1}",
obj.product.Name,
obj.price.UnitPrice);
}
我还有一些对象/值要添加到 foreach 循环中的每个单独的结果对象。
我无法做到这一点,因为它会引发运行时错误,如下所示:
<>f__AnonymousType1<dynamic,dynamic>' does not contain a definition for 'Test' and no extension method 'Test' accepting a first argument of type '<>f__AnonymousType1<dynamic,dynamic>' could be found (are you missing a using directive or an assembly reference?)