w3resource logo


C# Sharp Programming Exercises

C# Sharp programming Exercises, Practice, Solution : LINQ

Secondary Nav

C# Sharp LINQ [30 exercises with solution]

1. Write a program in C# Sharp to shows how the three parts of a query operation execute. Go to the editor
Expected Output :
The numbers which produce the remainder 0 after divided by 2 are :
0 2 4 6 8
Click me to see the solution

2. Write a program in C# Sharp to find the +ve numbers from a list of numbers using two where conditions in LINQ Query. Go to the editor
Expected Output :
The numbers within the range of 1 to 11 are :
1 3 6 9 10
Click me to see the solution

3. Write a program in C# Sharp to find the number of an array and the square of each number. Go to the editor
Expected Output :
{ Number = 9, SqrNo = 81 }
{ Number = 8, SqrNo = 64 }
{ Number = 6, SqrNo = 36 }
{ Number = 5, SqrNo = 25 }
Click me to see the solution

4. Write a program in C# Sharp to display the number and frequency of number from giving array. Go to the editor
Expected Output :
The number and the Frequency are :
Number 5 appears 3 times
Number 9 appears 2 times
Number 1 appears 1 times
. . . .
Click me to see the solution

5. Write a program in C# Sharp to display the characters and frequency of character from giving string. Go to the editor
Test Data :
Input the string : apple
Expected Output :
The frequency of the characters are :
Character a: 1 times
Character p: 2 times
Character l: 1 times
Character e: 1 times
Click me to see the solution

6. Write a program in C# Sharp to display the name of the days of a week. Go to the editor
Expected Output :
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Click me to see the solution

7. Write a program in C# Sharp to display numbers, multiplication of number with frequency and frequency of a number of giving array. Go to the editor
Test Data :
The numbers in the array are :
5, 1, 9, 2, 3, 7, 4, 5, 6, 8, 7, 6, 3, 4, 5, 2
Expected Output :
Number Number*Frequency Frequency
------------------------------------------------
5 15 3
1 1 1
9 9 1
2 4 2
. . . . . . . .
Click me to see the solution

8. Write a program in C# Sharp to find the string which starts and ends with a specific character. Go to the editor
Test Data :
The cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI','AMSTERDAM','ABU DHABI','PARIS'
Input starting character for the string : A
Input ending character for the string : M
Expected Output :
The city starting with A and ending with M is : AMSTERDAM
Click me to see the solution

9. Write a program in C# Sharp to create a list of numbers and display the numbers greater than 80 as output. Go to the editor
Test Data :
The members of the list are :
55 200 740 76 230 482 95
Expected Output :
The numbers greater than 80 are :
200
740
230
482
95
Click me to see the solution

10. Write a program in C# Sharp to Accept the members of a list through the keyboard and display the members more than a specific value. Go to the editor
Test Data :
Input the number of members on the List : 5
Member 0 : 10
Member 1 : 48
Member 2 : 52
Member 3 : 94
Member 4 : 63
Input the value above you want to display the members of the List : 59
Expected Output :
The numbers greater than 59 are :
94
63
Click me to see the solution

11. Write a program in C# Sharp to display the top n-th records. Go to the editor
Test Data :
The members of the list are :
5
7
13
24
6
9
8
7
How many records you want to display ? : 3
Expected Output :
The top 3 records from the list are :
24
13
9
Click me to see the solution

12. Write a program in C# Sharp to find the uppercase words in a string. Go to the editor
Test Data :
Input the string : this IS a STRING
Expected Output :
The UPPER CASE words are :
IS
STRING
Click me to see the solution

13. Write a program in C# Sharp to convert a string array to a string. Go to the editor
Test Data :
Input number of strings to store in the array :3
Input 3 strings for the array :
Element[0] : cat
Element[1] : dog
Element[2] : rat
Expected Output :
Here is the string below created with elements of the above array :
cat, dog, rat
Click me to see the solution

14. Write a program in C# Sharp to find the n-th Maximum grade point achieved by the students from the list of students. Go to the editor
Test Data :
Which maximum grade point(1st, 2nd, 3rd, ...) you want to find : 3
Expected Output :
Id : 7, Name : David, achieved Grade Point : 750
Id : 10, Name : Jenny, achieved Grade Point : 750
Click me to see the solution

15. Write a program in C# Program to Count File Extensions and Group it using LINQ. Go to the editor
Test Data :
The files are : aaa.frx, bbb.TXT, xyz.dbf,abc.pdf
aaaa.PDF,xyz.frt, abc.xml, ccc.txt, zzz.txt
Expected Output :
Here is the group of extension of the files :
1 File(s) with .frx Extension
3 File(s) with .txt Extension
1 File(s) with .dbf Extension
2 File(s) with .pdf Extension
1 File(s) with .frt Extension
1 File(s) with .xml Extension
Click me to see the solution

16. Write a program in C# Sharp to Calculate Size of File using LINQ. Go to the editor
Expected Output :
The Average file size is 3.4 MB
Click me to see the solution

17. Write a program in C# Sharp to Remove Items from List using remove function by passing the object. Go to the editor
Test Data :
Here is the list of items :
Char: m
Char: n
Char: o
Char: p
Char: q
Expected Output :
Here is the list after removing the item 'o' from the list :
Char: m
Char: n
Char: p
Char: q
Click me to see the solution

18. Write a program in C# Sharp to Remove Items from List by creating an object internally by filtering. Go to the editor
Test Data :
Here is the list of items :
Char: m
Char: n
Char: o
Char: p
Char: q
Expected Output :
Here is the list after removing the item 'p' from the list :
Char: m
Char: n
Char: o
Char: q
Click me to see the solution

19. Write a program in C# Sharp to Remove Items from List by passing filters. Go to the editor
Test Data :
Here is the list of items :
Char: m
Char: n
Char: o
Char: p
Char: q
Expected Output :
Here is the list after removing item 'q' from the list :
Char: m
Char: n
Char: o
Char: p
Click me to see the solution

20. Write a program in C# Sharp to Remove Items from List by passing the item index. Go to the editor
Test Data :
Here is the list of items :
Char: m
Char: n
Char: o
Char: p
Char: q
Expected Output :
Here is the list after removing item index 3 from the list :
Char: m
Char: n
Char: o
Char: q
Click me to see the solution

21. Write a program in C# Sharp to remove a range of items from a list by passing the start index and number of elements to remove. Go to the editor
Test Data :
Here is the list of items :
Char: m
Char: n
Char: o
Char: p
Char: q
Expected Output :
Here is the list after removing the three items starting from the item index 1 from the list :
Char: m
Char: q
Click me to see the solution

22. Write a program in C# Sharp to find the strings for a specific minimum length. Go to the editor
Test Data :
Input number of strings to store in the array :4
Input 4 strings for the array :
Element[0] : this
Element[1] : is
Element[2] : a
Element[3] : string
Input the minimum length of the item you want to find : 5
Expected Output :
The items of minimum 5 characters are :
Item: string
Click me to see the solution

23. Write a program in C# Sharp to generate a Cartesian Product of two sets. Go to the editor
Expected Output :
The Cartesian Product are :
{ letterList = X, numberList = 1 }
{ letterList = X, numberList = 2 }
{ letterList = X, numberList = 3 }
{ letterList = X, numberList = 4 }
. . .
Click me to see the solution

24. Write a program in C# Sharp to generate a Cartesian Product of three sets. Go to the editor
Expected Output :
The Cartesian Product are :
{ letter = X, number = 1, colour = Green }
{ letter = X, number = 1, colour = Orange }
{ letter = X, number = 2, colour = Green }
{ letter = X, number = 2, colour = Orange }
{ letter = X, number = 3, colour = Green }
{ letter = X, number = 3, colour = Orange }
{ letter = Y, number = 1, colour = Green }
{ letter = Y, number = 1, colour = Orange }
. . .
Click me to see the solution

25. Write a program in C# Sharp to generate an Inner Join between two data sets. Go to the editor
Expected Output :

Item ID         Item Name       Purchase Quantity                                                             
-------------------------------------------------------                                                       
1               Biscuit                 458                                                                   
2               Chocolate               650                                                                   
3               Butter                  800                                                                   
3               Butter                  900                                                                   
3               Butter                  900                                                                   
4               Brade                   700                                                                   
4               Brade                   650

Click me to see the solution

26. Write a program in C# Sharp to generate a Left Join between two data sets. Go to the editor
Expected Output :

Here is the list after joining  :                                                                             
                                                                                                              
Item ID         Item Name       Purchase Quantity                                                             
-------------------------------------------------------                                                       
1               Biscuit                 458                                                                   
2               Chocolate               650                                                                   
3               Butter                  800                                                                   
3               Butter                  900                                                                   
3               Butter                  900                                                                   
4               Brade                   700                                                                   
4               Brade                   650                                                                   
5               Honey                   0   

Click me to see the solution

27. Write a program in C# Sharp to generate a Right Outer Join between two data sets. Go to the editor
Expected Output :

Here is the list after joining  :                                                                             
                                                                                                              
Item ID         Item Name       Purchase Quantity                                                             
-------------------------------------------------------                                                       
3               Butter                  800                                                                   
5               Honey                   650                                                                   
3               Butter                  900                                                                   
4               Brade                   700                                                                   
3               Butter                  900                                                                   
4               Brade                   650                                                                   
1               Biscuit                 458   

Click me to see the solution

28. Write a program in C# Sharp to display the list of items in the array according to the length of the string then by name in ascending order. Go to the editor
Expected Output :
Here is the arranged list :
ROME
PARIS
LONDON
ZURICH
NAIROBI
ABU DHABI
AMSTERDAM
NEW DELHI
CALIFORNIA
Click me to see the solution

29. Write a program in C# Sharp to split a collection of strings into some groups. Go to the editor
Expected Output :
Here is the group of cities :

ROME; LONDON; NAIROBI
-- here is a group of cities --

CALIFORNIA; ZURICH; NEW DELHI
-- here is a group of cities --

AMSTERDAM; ABU DHABI; PARIS
-- here is a group of cities --

NEW YORK
-- here is a group of cities --
Click me to see the solution

30. Write a program in C# Sharp to arrange the distinct elements in the list in ascending order. Go to the editor
Expected Output :
Biscuit
Brade
Butter
Honey
Click me to see the solution

C# Sharp Practice online





List of Exercises with Solutions :

´╗┐

Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables