在下面的效用函数(列表到数据表)中,如何排除某些列表成员。
喜欢,我不想发送2个属性“UniqueKey”& “PointToPointData”到下面的实用方法,
public class PointDataClone
{
public int DataId { get; set; }
public string UniqueKey { get; set; }
public int Count { get; set; }
public List<PointToPointData> PointToPointData { get; set; }
}
效用函数,
public static DataTable ToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dataTable.Columns.Add(propertyDescriptor.Name, type);
}
object[] values = new object[propertyDescriptorCollection.Count];
foreach (T iListItem in iList)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
答案 0 :(得分:0)
创建属性
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class DontShowMe : Attribute
{
}
然后,您可以使用该属性为您的班级注释
public class PointDataClone
{
public int DataId { get; set; }
[DontShowMe]
public string UniqueKey { get; set; }
public int Count { get; set; }
[DontShowMe]
public List<PointToPointData> PointToPointData { get; set; }
}
并修改您的函数以查询属性。您将需要一个额外的使用声明
using System.ComponentModel;
将此行添加到循环
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) type = Nullable.GetUnderlyingType(type);
// test attribute to see if it is shown
if (propertyDescriptor.Attributes.Contains(new DontShowMe())) continue;
dataTable.Columns.Add(propertyDescriptor.Name, type);
您现在必须处理这样一个事实,即您的对象将具有比数据表具有列更多的属性。我会留给你管理那个小细节。
希望这有帮助,
马克
答案 1 :(得分:0)
请在下面找到代码。 我传递了字符串列表以排除属性名称。 参见下面的代码。
public static DataTable ToDataTableVendorExp<T>(List<T> items,List<string> ExcludeColumnName)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(a => a.PropertyType != typeof(System.Object)).Cast<PropertyInfo>().ToArray();
List<int> lstIgnoreIndex = new List<int>();
int colIndex = 0;
foreach (PropertyInfo prop in Props)
{
//Defining type of data column gives proper data table
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
//Setting column names as Property names
string dtcolumname = prop.Name;
//check if property name of list dont need to export then ignore.
if (ExcludeColumnName != null && ExcludeColumnName.IndexOf(dtcolumname) >= 0)
{
lstIgnoreIndex.Add(colIndex);
}
else
{
if (Attribute.IsDefined(prop, typeof(DisplayNameRptAttribute)))
{
var attrvalue = (DisplayNameRptAttribute)prop.GetCustomAttribute(typeof(DisplayNameRptAttribute));
dtcolumname = attrvalue.GetName();
}
dataTable.Columns.Add(dtcolumname, type);
}
colIndex++;
}
//if ignore 3 properties out of 10 then only 7 required.
int totlaproplength = Props.Length;
if (lstIgnoreIndex.Count > 0)
totlaproplength = totlaproplength - lstIgnoreIndex.Count;
foreach (T item in items)
{
var values = new object[totlaproplength];
for (int i = 0; i < Props.Length; i++)
{
if (lstIgnoreIndex.IndexOf(i) >= 0)
continue;
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
if (Props[i].ToString() == "System.DateTime Date")
{
values[i] = String.Format("{0:MM/dd/yyyy}", (Props[i].GetValue(item, null)));
}
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}