1. Sample code
using System; using ; using ; public class Program { public static void Main() { // Create some sample entity objects var people = new List<Person> { new Person { Name = "Alice", Age = 30, City = "New York" }, new Person { Name = "Bob", Age = 25, City = "Los Angeles" }, new Person { Name = "Alice", Age = 30, City = "New York" },//Repeat new Person { Name = "Charlie", Age = 35, City = "Chicago" }, new Person { Name = "Alice", Age = 28, City = "San Francisco" } }; // 1. Single field deduplication var uniqueNameFields = (p =>).ToList(); ("Specify the field (Name) to deduplicate the result, and if the duplication is repeated, the first item is retained:"); foreach (var person in uniqueNameFields) { ($"Name: {}, Age: {}, City: {}"); } // 2. Multi-field deduplication var uniqueNameAgeFields = (p => new { , }).ToList(); ("\nSpecify the field (Name, Age) to deduplicate the result, and if the duplication is repeated, the first item is retained:"); foreach (var person in uniqueNameAgeFields) { ($"Name: {}, Age: {}, City: {}"); } //3. De-repeat all fields // Press Name and Age fields through GroupBy to re-repeat var uniquePeople = (p => new { , , }).ToList(); ("\nFull fields deduplication:"); foreach (var person in uniquePeople) { ($"Name: {}, Age: {}, City: {}"); } } } public class Person { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } }
2. Sample output
If the specified field (Name) is deduplicated, the first item will be retained if it is repeated:
Name: Alice, Age: 30, City: New York
Name: Bob, Age: 25, City: Los Angeles
Name: Charlie, Age: 35, City: Chicago
The deduplication result is specified in the field (Name, Age), and the first item is retained if it is repeated:
Name: Alice, Age: 30, City: New York
Name: Bob, Age: 25, City: Los Angeles
Name: Charlie, Age: 35, City: Chicago
Name: Alice, Age: 28, City: San Francisco
Deduplication for all fields:
Name: Alice, Age: 30, City: New York
Name: Bob, Age: 25, City: Los Angeles
Name: Charlie, Age: 35, City: Chicago
Name: Alice, Age: 28, City: San Francisco
3. Pay attention to thunder points
The following code cannot complete the full field deduplication because people are reference types. Distinct() is generally used for deduplication of the value types such as List<string> and List<int>, and does not involve field comparisons of reference types.
().ToList()
If you need to deduplicate all fields: 1. Use the DinstinctBy syntax and add all fields. 2. Use the encapsulation method of title 4 (reflection to achieve full-field deduplication).
4. Full field deduplication encapsulation method
1. Packaging
/// <summary> /// General method of full field deduplication/// </summary> /// <returns></returns> public static IEnumerable<T> DistinctByAllFields<T>(IEnumerable<T> items) { // Get all field values of type T var properties = typeof(T).GetProperties( | ); return items .GroupBy(item => (",", (p => (item)))) // Concatenate by all field values to generate unique identifiers .Select(group => ()); // Take the first element of each group}
2. Example
using System; using ; using ; using ; public class Program { public static void Main() { // Create some sample entity objects var people = new List<Person> { new Person { Name = "Alice", Age = 30, City = "New York" }, new Person { Name = "Bob", Age = 25, City = "Los Angeles" }, new Person { Name = "Alice", Age = 30, City = "New York" }, new Person { Name = "Charlie", Age = 35, City = "Chicago" }, new Person { Name = "Alice", Age = 28, City = "San Francisco" } }; // Call the encapsulated deduplication method var uniquePeople = DistinctByAllFields(people).ToList(); ("Results based on all fields:"); foreach (var person in uniquePeople) { ($"Name: {}, Age: {}, City: {}"); } } /// <summary> /// General method of full field deduplication /// </summary> /// <returns></returns> public static IEnumerable<T> DistinctByAllFields<T>(IEnumerable<T> items) { // Get all field values of type T var properties = typeof(T).GetProperties( | ); return items .GroupBy(item => (",", (p => (item)))) // Concatenate by all field values to generate unique identifiers .Select(group => ()); // Take the first element of each group } } public class Person { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } }
The above is the detailed content of C# using Linq to achieve simple deduplication processing. For more information about C# Linq deduplication, please pay attention to my other related articles!