score:2
Accepted answer
You need to use reflection for this. This works:
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;
}
}
Sample data:
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();
Source: stackoverflow.com
Related Articles
- Intersect two generic lists by dynamic properties
- How to Create a generic method with 1 known property. Want to dynamic get the properties in different class with Generic Class property
- Entity Framework dynamic linq where from generic source with dynamic where clause
- Quickest way to compare two generic lists for differences
- How to combine more than two generic lists in C# Zip?
- intersect two lists with different objects
- Use LINQ to select distinct properties in Lists of Lists
- Dynamic linq order by on nested property with null properties
- passing dynamic expression to order by in code first EF repository
- Intersect between two lists not working
- Access nested properties with dynamic lambda using Linq.Expression
- Visual Studio Code Analysis Rule - "Do not expose generic lists"
- Merging 2 lists and sum several properties using LINQ
- Search through Where clause in LINQ, using dynamic properties
- Compare two generic lists and remove duplicates
- LINQ Source Code Available
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- Creating an object with dynamic properties in C#
- Merge 2 lists based on one property and concatenate other properties
- .NET 4 Code Contracts: "requires unproven: source != null"
- Intersect two lists and return the similarity with the preserved order of the original first string value
- LINQ expression with generic class properties
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- Create a Dynamic Linq to EF Expression to Select IQueryable into new class and assign properties
- Linq intersect or join to return items from one collection that have matching properties to another?
- Sort JSON by one or more properties using Dynamic LINQ
- C# - Dynamic Linq left outer join on multiple properties
- Generic ValidationAttribute with dynamic Entity.Property unique check (set at run time)
- Intersect lists on KeyValuePair key?
- Compare two generic Lists
- LINQ flavored IS IN Query
- Select Where Table.Value == Maximum
- LINQ to XML. How to get some string?
- How to use async and await in LINQ?
- LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)'
- Get resulting LINQ expression from LINQ Dynamic WHERE
- Effective way in LINQ of joining based on index
- Compare two generic lists using Linq
- The 'in' predicate in Entity Framework
- LINQ inner join betwenn Enumerable and DB Table
- Is ToList required when using foreach with LINQ to Entities
- How to retrieve element from IQueryable in Parallel Loop
- LINQ query to sort list of numeric strings with NULLs
- Linq TypeScript Ternary Operator - is it possible to re-use the condition as the first expression without duplicating code?
- LINQ to Entities Question - All objects where all items in subcollection appear in another collection?
- Linq groupby with where clause
- Compile Expression at runtime Using Serialize.Linq
- Can't use LINQ's Expression Overload of MongoDB.Driver Include ProjectionDefinition
- Linq To Sql compare Time only
- Test if an IEnumerable<T> contains 1 element without counting or using Single