c# – 按动态属性相交两个通用列表

我有两个通用列表,有几个属性可供比较,但我希望密钥标识符是List< string>动态的.

所以我们说我们有这样的课程:

class A
{
    string Name { get; set; }
    string Color1 { get; set; }
    string Color2 { get; set; }
    string Length { get; set; }
}

用户现在可以从用户界面中选择这些对象的两个列表的属性需要重叠,以便选择正确的对.它存储在List< string>中.例如,如果列表字符串包含“Name”和“Color1”,则只返回“Name”和“Color1”重叠的对象.

我试图编写一个函数,但不幸的是我不确定我应该将通用列表转换为哪个集合,以及如何应用这些属性的名称?如果“标识符”的名称始终相同,则Linq / Lambda不会出现问题;)

提前致谢

最佳答案 你需要使用反射.这有效:

public class A
{
    public string Name { get; set; }
    public string Color1 { get; set; }
    public string Color2 { get; set; }
    public string Length { get; set; }

    public static IEnumerable<A> Intersecting(IEnumerable<A> input, List<string> propertyNames)
    { 
        if(input == null)
            throw new ArgumentNullException("input must not be null ", "input");
        if (!input.Any() || propertyNames.Count <= 1)
            return input;

        var properties = typeof(A).GetProperties();
        var validNames = properties.Select(p => p.Name);
        if (propertyNames.Except(validNames, StringComparer.InvariantCultureIgnoreCase).Any())
            throw new ArgumentException("All properties must be one of these: " + string.Join(",", validNames), "propertyNames");

        var props = from prop in properties
                    join name in validNames.Intersect(propertyNames, StringComparer.InvariantCultureIgnoreCase)
                    on prop.Name equals name
                    select prop;
        var allIntersecting = input
            .Select(a => new { 
                Object = a,
                FirstVal = props.First().GetValue(a, null),
                Rest = props.Skip(1).Select(p => p.GetValue(a, null)),
            })
            .Select(x => new { 
                x.Object, x.FirstVal, x.Rest,
                UniqueValues = new HashSet<object>{ x.FirstVal }
            })
            .Where(x => x.Rest.All(v => !x.UniqueValues.Add(v)))
            .Select(x => x.Object);
        return allIntersecting;
    }
}

样本数据:

var aList = new List<A> { 
    new A { Color1 = "Red", Length = "2", Name = "Red" }, new A { Color1 = "Blue", Length = "2", Name = "Blue" },
    new A { Color1 = "Red", Length = "2", Name = "A3" }, new A { Color1 = "Blue", Length = "2", Name = "A3" },
    new A { Color1 = "Red", Length = "3", Name = "Red" }, new A { Color1 = "Blue", Length = "2", Name = "A6" },
};
var intersecting = A.Intersecting(aList, new List<string> { "Color1", "Name" }).ToList();
点赞