Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to get distinct element from a sequence
Set a sequence and add elements.
List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 };
Use Distinct() method to get distinct element from the above list.
IEnumerable<int> res = ID.AsQueryable().Distinct();
Let us see the complete code.
Example
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 };
// get distinct elements
IEnumerable<int> res = ID.AsQueryable().Distinct();
foreach (int arr in res) {
Console.WriteLine(arr);
}
}
}
Output
120 111 250 300 399 450
Advertisements