c# - Select All distinct values in a column using LINQ

C# - Select All distinct values in a column using LINQ

In LINQ, you can select all distinct values in a column using the Distinct method. Here's how you can do it:

using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main(string[] args) { // Sample data List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 1, 2, 3 }; // Select distinct values using LINQ IEnumerable<int> distinctNumbers = numbers.Distinct(); // Print distinct values Console.WriteLine("Distinct numbers:"); foreach (int number in distinctNumbers) { Console.WriteLine(number); } } } 

In this example:

  • We have a list of integers (numbers) containing some duplicate values.
  • We use the Distinct method to select all distinct values from the list.
  • The result (distinctNumbers) is an IEnumerable<int> containing only the distinct values.
  • We then iterate over the distinct values and print them.

You can apply the same approach to any other collection or data source in LINQ to select distinct values from a column. Just replace numbers with your collection or query result.

Examples

  1. C# LINQ select distinct values from column:

    • Description: How to use LINQ to select all distinct values from a specific column in a collection or dataset in C#.
    • Code:
      using System; using System.Linq; class Program { static void Main(string[] args) { var data = new[] { "apple", "banana", "apple", "orange", "banana" }; var distinctValues = data.Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  2. C# LINQ select distinct values from column in list:

    • Description: How to use LINQ to select all unique values from a specific column within a list or IEnumerable in C#.
    • Code:
      using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var data = new List<string> { "apple", "banana", "apple", "orange", "banana" }; var distinctValues = data.Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  3. C# LINQ select distinct values from column in datatable:

    • Description: Using LINQ to select all distinct values from a specific column within a DataTable object in C#.
    • Code:
      using System; using System.Data; using System.Linq; class Program { static void Main(string[] args) { var dt = new DataTable(); dt.Columns.Add("Fruits", typeof(string)); dt.Rows.Add("apple"); dt.Rows.Add("banana"); dt.Rows.Add("apple"); dt.Rows.Add("orange"); dt.Rows.Add("banana"); var distinctValues = dt.AsEnumerable() .Select(row => row.Field<string>("Fruits")) .Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  4. C# LINQ select distinct values from column in database:

    • Description: How to use LINQ to select all distinct values from a specific column in a database table using Entity Framework or LINQ to SQL in C#.
    • Code:
      using System; using System.Linq; class Program { static void Main(string[] args) { using (var context = new YourDbContext()) { var distinctValues = context.YourTable .Select(x => x.ColumnName) .Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } } 
  5. C# LINQ select distinct values from column in array:

    • Description: How to use LINQ to select all distinct values from a specific column within an array in C#.
    • Code:
      using System; using System.Linq; class Program { static void Main(string[] args) { var data = new[] { new { Fruit = "apple" }, new { Fruit = "banana" }, new { Fruit = "apple" }, new { Fruit = "orange" }, new { Fruit = "banana" } }; var distinctValues = data.Select(x => x.Fruit).Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  6. C# LINQ select distinct values from column in list of objects:

    • Description: How to use LINQ to select all distinct values from a specific property within a list of objects in C#.
    • Code:
      using System; using System.Collections.Generic; using System.Linq; class Fruit { public string Name { get; set; } } class Program { static void Main(string[] args) { var fruits = new List<Fruit> { new Fruit { Name = "apple" }, new Fruit { Name = "banana" }, new Fruit { Name = "apple" }, new Fruit { Name = "orange" }, new Fruit { Name = "banana" } }; var distinctValues = fruits.Select(x => x.Name).Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  7. C# LINQ select distinct values from column with condition:

    • Description: Using LINQ to select distinct values from a specific column in C# based on certain conditions.
    • Code:
      using System; using System.Linq; class Program { static void Main(string[] args) { var data = new[] { "apple", "banana", "apple", "orange", "banana" }; var distinctValues = data.Where(x => x.StartsWith("a")).Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  8. C# LINQ select distinct values from column with orderby:

    • Description: Using LINQ to select distinct values from a specific column in C# and ordering the result.
    • Code:
      using System; using System.Linq; class Program { static void Main(string[] args) { var data = new[] { "apple", "banana", "apple", "orange", "banana" }; var distinctValues = data.Distinct().OrderBy(x => x); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 
  9. C# LINQ select distinct values from column with case insensitive comparison:

    • Description: Selecting distinct values from a specific column in C# with case-insensitive comparison using LINQ.
    • Code:
      using System; using System.Linq; class Program { static void Main(string[] args) { var data = new[] { "apple", "banana", "Apple", "orange", "banana" }; var distinctValues = data.Select(x => x.ToLower()).Distinct(); foreach (var value in distinctValues) { Console.WriteLine(value); } } } 

More Tags

contain linq-to-sql media posix jwk nodemailer nuxt.js hdf5 scrolltop class-validator

More Programming Questions

More Chemical thermodynamics Calculators

More Electrochemistry Calculators

More Bio laboratory Calculators

More Auto Calculators