score:2
Accepted answer
Try to avoid reflection because it can slow down your application. As an alternative you can create a dictionary and put all comparators into it:
var configDictionary = new Dictionary<string, List<Func<object, object, bool>>>
{
{
"Book",
new List<Func<object, object, bool>>
{
(b1, b2) => ((Book)b1).Title == ((Book)b2).Title,
(b1, b2) => ((Book)b1).CoverImage == ((Book)b2).CoverImage,
(b1, b2) => ((Book)b1).NumberOfPages == ((Book)b2).NumberOfPages,
(b1, b2) => ((Book)b1).Id != ((Book)b2).Id,
}
},
// same for Authors
};
Now you can use it in Where
method:
var typeName = obj.GetType().Name; // here we using Reflection but once per collection, not per each item
var first = someList.Where(x => configDictionary[typeName].All(f => f(x, obj))).FirstOrDefault();
Also, because FirstOrDefault
also has overload that accept predicate last line can be rewritten to:
var first = someList.FirstOrDefault(x => configDictionary[typeName].All(f => f(x, obj)));
score:0
A better solution will be creating custom attribute which will tag property. Then in class override default method Equals which will get all properties with this attribute and return equality.
Source: stackoverflow.com
Related Articles
- How use Reflection to condition multiple properties to check for equality in a LINQ .Where statement, depending on what class is passed?
- How to sort list on multiple properties in one line of code in Linq
- linq check what condition return false in multiple and-conditions
- Check if multiple sub lists are present in a source list
- Set multiple properties in a List<T> ForEach()?
- linq distinct or group by multiple properties
- Using LINQ to find duplicates across multiple properties
- Using LINQ to group by multiple properties and sum
- What is the best way to check two List<T> lists for equality in C#
- Check IEnumerable<T> for items having duplicate properties
- anonymous type and multiple properties
- LINQ: differences between single Where with multiple conditions and consecutive Wheres with single condition
- LINQ Group by with multiple properties in VB.Net
- How to use linq `Except` with multiple properties with different class?
- Efficient way to call .Sum() on multiple properties
- Does this LINQ code perform multiple lookups on the original data?
- LINQ - SelectMany from multiple properties in same object
- Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
- Linq where clause with multiple conditions and null check
- How to reuse a linq expression for 'Where' when using multiple source tables
- C# - Combine multiple LINQ collections with same properties
- How to use IEnumerable.GroupBy comparing multiple properties between elements?
- Groupby multiple date properties by month and year in LINQ
- LINQ Source Code Available
- multiple orderby in this linq code
- Linq Query to Group By Multiple Columns But Get Additional Properties
- .NET 4 Code Contracts: "requires unproven: source != null"
- Get properties of class by order using reflection
- LINQ Check List<string> for multiple entries
- LInq left join with multiple condition in on clause
- Difference between lambda and LINQ?
- Getting tag values in XML separately
- Linq to SQL construct a custom object externally - join from another object
- How to add List to a Key in a Linq to Sql Group By
- group by in Linq to entities
- How to sort the second column of csv file using LINQ query in Power shell
- Convert SQL query to LINQ
- Filter LINQ To Entities (EF Core) query by List of HashSet of String and Enum
- Sorting an ArrayList
- LINQ query expressions that operate on types (monads?) other than IEnumerable<T> -- Possible uses?
- How to remove error “Cannot compare elements of type 'System.Linq.IGrouping`2”
- Convert to date Format
- Facing an issue with Add or Update scenario
- Linq Combine Left Join Data
- How to apply expression tree with an object with null options
- What will GroupBy in LINQ do if it has two parameters?
- Entity Framework Data Transfer Objects Best Practice
- Linq select strange effect
- Writing a nested join with LINQ to Entities
- Using c# GetFiles Length but only count the files with certain amount of chars in filename