To get distinct records containing certain keywords using LINQ in C#, you can use the Where and Distinct extension methods in combination with the Contains method.
Here's an example:
var records = new List<string> { "The quick brown fox", "jumped over the lazy dog", "The quick brown cat", "The quick brown bird", "flew over the lazy dog" }; var keywords = new List<string> { "quick", "dog" }; var result = records .Where(r => keywords.Any(k => r.Contains(k))) .Distinct(); foreach (var item in result) { Console.WriteLine(item); } In this example, we have a list of records and a list of keywords. We use the Where method to filter the records that contain any of the specified keywords using the Contains method and the Any method to check if any of the keywords are present.
We then use the Distinct method to remove any duplicate records from the filtered result.
Finally, we print out the distinct records containing the specified keywords using a foreach loop.
Note that the Contains method is case-sensitive, so you may need to use a case-insensitive comparison method such as StringComparison.OrdinalIgnoreCase if you want to perform a case-insensitive search.
"LINQ Distinct Records Containing Keywords"
var result = collection .Where(item => item.Description.Split(' ').Any(keyword => keywords.Contains(keyword))) .Distinct(); "C# LINQ: Distinct Records with Case-Insensitive Keyword Matching"
var result = collection .Where(item => item.Description.Split(' ').Any(keyword => keywords.Contains(keyword, StringComparer.OrdinalIgnoreCase))) .Distinct(); "LINQ Distinct Records with Custom Keyword Comparer"
var result = collection .Where(item => item.Description.Split(' ').Any(keyword => keywords.Contains(keyword, new CustomComparer()))) .Distinct(); "C# LINQ: Distinct Records Containing All Keywords"
var result = collection .Where(item => keywords.All(keyword => item.Description.Contains(keyword))) .Distinct();
"LINQ Distinct Records with Keyword Count"
var result = collection .Where(item => item.Description.Split(' ').Count(keyword => keywords.Contains(keyword)) > 0) .Distinct(); "C# LINQ: Distinct Records with Minimum Keyword Match"
var result = collection .Where(item => item.Description.Split(' ').Count(keyword => keywords.Contains(keyword)) >= minKeywordMatch) .Distinct(); "LINQ Distinct Records with Keyword Exclusion"
var result = collection .Where(item => !item.Description.Split(' ').Any(keyword => excludedKeywords.Contains(keyword))) .Distinct(); "C# LINQ: Distinct Records with Keyword Matching and Projection"
var result = collection .Where(item => item.Description.Split(' ').Any(keyword => keywords.Contains(keyword))) .Select(item => new { item.Property1, item.Property2 }) .Distinct(); "LINQ Distinct Records with Keyword Grouping"
var result = collection .GroupBy(item => string.Join(",", item.Description.Split(' ').Intersect(keywords))) .Select(group => group.First()); "C# LINQ: Distinct Records with Keyword Matching and Case-Insensitive Projection"
var result = collection .Where(item => item.Description.Split(' ').Any(keyword => keywords.Contains(keyword, StringComparer.OrdinalIgnoreCase))) .Select(item => new { item.Property1, item.Property2 }) .Distinct(); angularjs-directive inheritance firemonkey setuptools lldb build.gradle percentile egit atlassian-sourcetree transducer