我从服务中获取数据表。我正在使用 linq 来计算总和新列。我想要数据表中的所有原始数据加上在 linq 中计算的新列。
问题是数据表中的列不固定。我将如何在 linq 的 select 子句中动态添加列。
下面是代码片段:
DataTable dt = ds.Tables[0];
var orderCtr =
from o in dt.AsEnumerable()
where o.Field<string>(Constants.GENDER_NAME) != "Unknown"
group o by new
{
odr_id = o.Field<int>(Constants.ORDER_ID),
//NEED TO ADD COLUMNS DYNAMICALLY HERE. MEANS IF THEY ARE IN DATATABLE.
}
into g
select new
{
//NEED TO ADD COLUMNS DYNAMICALLY HERE. MEANS IF THEY ARE IN DATATABLE.
odr_id = g.Key.odr_id,
ac_gr_imp = g.Sum(r => r.Field<long>(Constants.GENDER_IMPRESSION)),
ac_gr_clk = g.Sum(r => r.Field<long>(Constants.GENDER_CLICK)),
Ctr = (double)g.Sum(r => r.Field<long>(Constants.GENDER_IMPRESSION)) / g.Sum(r => r.Field<long>(Constants.GENDER_CLICK)),
};