所有,我最近本地化了整个应用程序,我面临以下问题;我的应用程序中有以下 LINQ 查询
var ccsum = from a in dtSumGL2.AsEnumerable()
group Math.Abs(a.Field<double>(strpcPSPassBegCost)) by new
{
BaseCC = a.Field<string>(strpcBaseCC)
}
into g
select new
{
g.Key.BaseCC,
PSPassBegCost = g.Sum()
};
这是创建一个新对象ccsum
,我们用它来创建DataTable
并随后填充 SQL Server 数据库。
问题是在 中创建的每个新项目都DataTable
带有列名BaseCC
和PSPassBegCost
,但这些名称与这些名称的德语版本不匹配。现在我的问题是:有没有办法做类似的事情:
var ccsum = from a in dtSumGL2.AsEnumerable()
group Math.Abs(a.Field<double>(strpcPSPassBegCost)) by new
{
BaseCC = a.Field<string>(strpcBaseCC)
}
into g
select new
{
g.Key.BaseCC as Resources.BaseCC,
PSPassBegCost = g.Sum() as Resources.PSPassBegCost
};
以便我可以根据它们的本地化名称命名表?
编辑。DataTable
从中检索 a 的代码ccsum
是
fooDt = Utils.LINQToDataTable(ccsum);
和
public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
PropertyInfo[] oProps = null;
if (varlist == null)
return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create
// table, Only first time, others will follow.
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
谢谢你的时间。
另一种方法可能是将ccsum
对象中的“列”重命名为后处理步骤,尽管sumcc
在运行时请求它们之前不会填充 的值 - 还有其他想法吗?