Docsity

C# LINQ Program examples, Assignments of Programming Languages

Toggle sidebar
bg1
Here are 10 exam-style LINQ programs/questions similar to:
"Using LINQ, display the cities whose names contain the letter 'e'."
1. Cities containing 'e'
Question
Using LINQ, display all cities whose names contain the letter 'e'.
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] cities =
{
"Lahore",
"Karachi",
"Peshawar",
"Quetta",
"Multan"
};
var result = cities.Where(c => c.Contains('e'));
foreach(var city in result)
Console.WriteLine(city);
}
}
2. Names starting with 'A'
Question
Display all student names that start with the letter 'A'.
string[] names =
{
"Ali",
"Ahmed",
"Sara",
"Ayesha",
"Usman"
pf3
pf4
pf5

Partial preview of the text

Download C# LINQ Program examples and more Assignments Programming Languages in PDF only on Docsity!

Here are 10 exam-style LINQ programs/questions similar to:

"Using LINQ, display the cities whose names contain the letter 'e'."

1. Cities containing 'e'

Question

Using LINQ, display all cities whose names contain the letter 'e'.

using System; using System.Linq; class Program { static void Main() { string[] cities = { "Lahore", "Karachi", "Peshawar", "Quetta", "Multan" }; var result = cities.Where(c => c.Contains('e')); foreach(var city in result) Console.WriteLine(city); } }

2. Names starting with 'A'

Question

Display all student names that start with the letter 'A'.

string[] names = { "Ali", "Ahmed", "Sara", "Ayesha", "Usman"

var result = names.Where(n => n.StartsWith("A")); foreach(var name in result) Console.WriteLine(name);

3. Even Numbers

Question

Display all even numbers using LINQ.

int[] nums = {1,2,3,4,5,6,7,8,9,10}; var result = nums.Where(n => n % 2 == 0); foreach(var n in result) Console.WriteLine(n);

4. Numbers Greater Than 50

Question

Display numbers greater than 50.

int[] nums = {10,20,55,70,90,35}; var result = nums.Where(n => n > 50); foreach(var n in result) Console.WriteLine(n);

5. Sort Names Alphabetically

Question

Sort student names in ascending order.

string[] names = { "Sara", "Ali", "Ahmed",

var result = nums.Distinct(); foreach(var n in result) Console.WriteLine(n);

9. Names Longer Than 5 Characters

Question

Display names whose length is greater than 5.

string[] names = { "Ali", "Ahmed", "Ayesha", "Usman", "Fatima" }; var result = names.Where(n => n.Length > 5); foreach(var n in result) Console.WriteLine(n);

10. Convert Names to Uppercase

Question

Display all names in uppercase using LINQ.

string[] names = { "Ali", "Ahmed", "Sara" }; var result = names.Select(n => n.ToUpper()); foreach(var n in result) Console.WriteLine(n);

11. Calculate Sum Using LINQ

Question

Find the sum of all numbers.

int[] nums = { 10,20,30,40, }; int sum = nums.Sum(); Console.WriteLine("Sum = " + sum);

Most Common LINQ Exam Patterns

Teachers frequently ask questions like:

Where() // filtering Select() // projection OrderBy() // sorting OrderByDescending() Take() Skip() Distinct() Count() Sum() Average() Max() Min() Any() All() Contains()

If you can solve the 10 examples above, you'll be able to handle most LINQ practical questions

in exams.