简体   繁体   English

通过与C#中的列表进行比较来忽略类的属性

[英]Ignore properties of class by comparing with a list in c#

I have a list where TestClass is a class with some predefined properties. 我有一个列表,其中TestClass是具有一些预定义属性的类。 So when i get data and bind my list with data i need to ignore some properties of TestClass by comparing it with a list. 因此,当我获取数据并将列表与数据绑定时,我需要通过将其与列表进行比较来忽略TestClass的某些属性。 How can i achieve that? 我该如何实现?

Below is my code 下面是我的代码

    public class TestClass
{
 public int id{get;set;}
 public string fname{get;set;} 
public string lname{get;set;}
public string job {get;set;}
public string role{get;set;}
public string address{get;set;}
}



List<TestClass> ulist = null;
   ulist = ToList<TestClass>(usersdataset.tables[0]); //fill my list with the data code is given below

so after getting the data into the list i need to remove some properties by comparing it with list of properties which should be returned.for example if my filteredlist should only show id,fname,role then i need to remove the extra properties from my ulist . 所以在将数据放入列表后,我需要通过将其与应该返回的属性列表进行比较来删除一些属性。例如,如果我的过滤列表仅显示id,fname,role,那么我需要从我的ulist中删除其他属性。 so after the filter ulist should only contain id,fname and role 因此,过滤器ulist之后应仅包含id,fname和角色

ToList Method ToList方法

public static List<T> ToList<T>(DataTable dataTable) where T : new()
        {
            var dataList = new List<T>();

            //Define what attributes to be read from the class
            const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

            //Read Attribute Names and Types
            var objFieldNames = typeof(T).GetProperties(flags).Cast<PropertyInfo>().
                Select(item => new
                {
                    Name = item.Name,
                    Type = Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType
                }).ToList();

            //Read Datatable column names and types
            var dtlFieldNames = dataTable.Columns.Cast<DataColumn>().
                Select(item => new {
                    Name = item.ColumnName,
                    Type = item.DataType
                }).ToList();

            foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
            {
                var classObj = new T();

                foreach (var dtField in dtlFieldNames)
                {
                    PropertyInfo propertyInfos = classObj.GetType().GetProperty(dtField.Name);

                    var field = objFieldNames.Find(x => x.Name == dtField.Name);
                    //var field = filteredColumns.Find(x => x.PropertyName == dtField.Name);
                    if (field != null)
                    {
                        if (dataRow[dtField.Name] != DBNull.Value)
                            propertyInfos.SetValue(classObj, dataRow[dtField.Name], null);
                    }   

                }                   
                dataList.Add(classObj);
            }
            return dataList;
        }

Use the overvride function Equals: This sample compare only the id property 使用重写函数等于:此示例仅比较id属性

public class TestClass
    {
        public int id { get; set; }
        public string fname { get; set; }
        public string lname { get; set; }
        public string job { get; set; }
        public string role { get; set; }
        public string address { get; set; }
        public override bool Equals(object obj)
        {
            if (obj.GetType().Name != this.GetType().Name)
            {
                return false;
            }
            TestClass testclassObject = (TestClass)obj;
            if (testclassObject.id != this.id)
            {
                return false;
            }

            return true;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM