0
我有一個項目,允許用戶通過左加入到List<T>
(它將通過輔助方法轉換爲DataTable
)將列添加到DataTable
。現在我有這個實現其工作:使用動態列數聯接數據表的功能方法
public static DataTable ListToDataTable<T>(this IList<T> data)
{
DataTable dt = new DataTable();
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
dt.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T t in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(t);
}
dt.Rows.Add(values);
}
return dt;
}
//for purposes of this demo, the first column in leftTable is always the
//column that's joined on
public static DataTable JoinWithList<T>(this DataTable leftTable, IList<T> data,string propertyToAdd)
{
var rightTable = new DataTable();
rightTable = data.ListToDataTable();
var joiningColumn = leftTable.Columns[0].ColumnName;
var columnIndex = 0;
//find the index of type T whose property is the same as leftTable's
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
if (prop.Name ==joiningColumn)
{
columnIndex = i;
}
}
var results = (from u in leftTable.AsEnumerable()
join s in rightTable.AsEnumerable()
on u[0] equals s[columnIndex] into ps
from p in ps.DefaultIfEmpty()
select new
{
MembershipId = u[0],
UserName = u[1],
//some sort of function to iterate over and add columns here
Salary = p == null ? 0.01 : p[propertyToAdd] = p[propertyToAdd]
}).ToList().ListToDataTable();
return results;
}
我希望能夠在List<string>
傳遞的最後一個參數和select new
塊內有一個可變數目appeneded新的匿名類型的列。
我與ML和F#完全陌生的。目前,你能否給這個ML提供一些C#僞代碼?這對我來說看起來很體面。 – wootscootinboogie
@wootscootinboogie,編輯添加:-) – Mau