Dynamically create anonymous object from list values c#

前端 未结 1 1753
栀梦
栀梦 2021-01-07 00:44

I have a list (or can be array) of strings that I want to dynamically create an anonymous object from. How do I do this?

var dataSet = new DataSet();
dataSet         


        
相关标签:
1条回答
  • 2021-01-07 01:02

    Stripping down your question, what you want is to be able to group a list of items based on a runtime property which could be composed of one or more properties of that item. In essence, it means you need a selector function (which is your Key method) that transforms an item into a key.

    In order for GroupBy to work, it needs to be able to compare any two instances of the key to see if they're equal. This means the key needs to implement a meaningful Equals() method, or you need an IEqualityComparer implementation that does the work for you. In this case I wouldn't bother with creating a new Key, just write an Equality Comparer that can compare two DataRows directly:

    var duplicates = dataTable
            .AsEnumerable()
            .GroupBy(r => r, new MyDataRowComparer(keys))
            .Where(c => c.Count() > 1)
            .ToList();
    
    
    internal class MyDataRowComparer : IEqualityComparer<DataRow>
    {
        private readonly string[] _keys;
    
        public MyDataRowComparer(string[] keys)
        {
            _keys = keys; // keep the keys to compare by.
        }
    
        public bool Equals(DataRow x, DataRow y)
        {
            // a simple implementation that checks if all the required fields 
            // match. This might need more work.
            bool areEqual = true;
            foreach (var key in _keys)
            {
                areEqual &= (x[key] == y[key]);
            }
            return areEqual;
        }
    
        public int GetHashCode(DataRow obj)
        {
            // Add implementation here to create an aggregate hashcode.
        }
    }    
    
    0 讨论(0)
提交回复
热议问题