我正在使用 WCF 和 EF 开发 Silverlight 应用程序。
我首先使用数据库,因为我们的数据库已经存在。
我有一个包含 100 个数据类型为 real 的列的表。我们想要生成一个类,该类在每列的类中都有一个List<double>
或而不是 100 个离散变量。List<float>
这可能吗 ??有人可以给我一个例子吗?
我正在使用 WCF 和 EF 开发 Silverlight 应用程序。
我首先使用数据库,因为我们的数据库已经存在。
我有一个包含 100 个数据类型为 real 的列的表。我们想要生成一个类,该类在每列的类中都有一个List<double>
或而不是 100 个离散变量。List<float>
这可能吗 ??有人可以给我一个例子吗?
没有直接的方法。您要做的是使用反射将其转换为List<double>
. 假设您的表名为MyObject
,那么 EF 将生成一个类MyObject
来表示该表中的一行。然后你可以这样做:
Type type = typeof(MyObject);
// Get properties (columns) through reflection
PropertyInfo[] properties = type.GetProperties();
List<List<double>> allRows = new List<List<double>>();
using(var dbContext = MyDB.GetContext())
{
foreach(var row in dbContext.MyRows)
{
List<double> columnValues = new List<double>();
foreach (PropertyInfo property in properties)
{
// The sql type REAL will map to either float or double
if(property.PropertyType == typeof(float))
{
columnValues.Add( (double) property.GetValue(row, null) );
}
}
allRows.Add(columnValues);
}
}
希望你能明白。