LINQ

  Home  Applications Programs  LINQ


“LINQ Interview Questions and Answers will guide us now that Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. LINQ defines a set of method names called standard query operators, or standard sequence operators, along with translation rules from so-called query expressions to expressions using these method names, so learn more abut LINQ with the help of this LINQ Interview Questions with Answers guide”



35 LINQ Questions And Answers

21⟩ Write a program using LINQ to find the sum of first 5 prime numbers?

class Program

{

static void Main()

{

int[] MyPrimeNumbers = {1,2,3,5,7};

// Use the Count() and Sum() Standard Query Operators to

// compute the count and total sum respectively

Concole.WriteLine("The sum of first {0} prime number is {1}",

MyPrimeNumbers.Count(), MyPrimeNumbers.Sum());

}

}

 237 views

22⟩ What are Quantifiers?

They are LINQ Extension methods which return a Boolean value

1) All

2) Any

3) Contains

4) SequenceEqual

example:

int[] arr={10,20,30};

var b=arr.All(a=>a>20);

-------------------------------------------

Output:

b will return False since all elements are not > 20.

 246 views

23⟩ What are the four LINQ Providers that .NET Framework ships?

1. LINQ to Objects - Executes a LINQ query against a collection of objects

2. LINQ to XML - Executes an XPATH query against XML documents

3. LINQ to SQL - Executes LINQ queries against Microsoft SQL Server.

4. LINQ to DataSets - Executes LINQ queries against ADO.NET DataSets.

 245 views

26⟩ How are Standard Query Operators useful in LINQ?

Standard Query Operators in LINQ can be used for working with collections for any of the following and more.

1. Get total count of elements in a collection.

2. Order the results of a collection.

3. Grouping.

4. Computing average.

5. Joining two collections based on matching keys.

6. Filter the results

 224 views

27⟩ Write a Program using Skip and Take operators. How can it beneficial for bulky data accessing on page?

“skip” and “take” Operator used for Paging. Suppose we have Customer table of 100 records. To find 10 records by skipping the first 50 records from customer table-

Public void Pagingdatasource ()

{

Var Query = from CusDetails in db.customer skip (50) Take (10)

ObjectDumper.Write(q)

}

Hence The LinQ “ SKIP” operator lets you skip the results you want, and “Take” Operator enables you yo select the rest of result . So By Using Skip and Take Operator, You can create paging of Specific sequence.

 267 views

28⟩ Write a Program for Concat to create one sequence of Data Rows that contains DataTabless Data Rows, one after the other?

(C#)

Public void Datasetlinq()

{

var numbersA = TestDS.Tables("NumbersA").AsEnumerable();

var numbersB = TestDS.Tables("NumbersB").AsEnumerable();

var allNumbers = numbersA.Concat(numbersB);

Console.WriteLine("All numbers from both arrays:");

foreach (object n_loopVariable in allNumbers)

{

n = n_loopVariable;

Console.WriteLine(n["number"]);

}

}

 255 views

30⟩ What is “OfType” in linq?

public void TypeofExa()

{

Var numbers = {null,1.0,"two", 3,"four",5,"six",7.0 };

var doubles = from n in numbers where n is doublen;

Console.WriteLine("Numbers stored as doubles:");

foreach (object d in doubles)

{

Console.WriteLine(d);

}

}

Output:

Numbers stored as doubles:

1

7

 228 views

32⟩ Differentiate between Conversion Operator “IEnumerable” and “ToDictionary” of LINQ?

IEnumerable and To Dictionary both are Conversion Operator which are used to solved to conversion type Problems.

“AsEnumerable ” operator simply returns the source sequence as an object of type IEnumerable. This kind of “conversion on the fly” makes it possible to call the general-purpose extension methods over source, even if its type has specific implementations of them

Signature:

public static IEnumerable<T> AsEnumerable<T>

(

this IEnumerable<T> source

);

“ToDictionary ” Conversion Operator is the instance of Dictionary (k,T) . The “keySelector ”predicate identifies the key of each item while “elementSelector ”, if provided, is used to extract each single item.

Key and elementSelector Predicate can be Used in following ways-

Example-

Public void ToDictionatyExample()

{

Var scoreRecords=

{

new {Name = "Alice",Score = 50 },

new {Name = "Bob",Score = 40 },

new {Name = "Cathy", Score = 45}

};

Var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);

Console.WriteLine("Bob's score: {0}", scoreRecordsDict("Bob"));

}

Result: Bob's score: { Name = Bob, Score = 40 }

 219 views

33⟩ Why do we use “Contains” method for strings type functions?

Contains method Used to find all matching records From Given string using Matching keywords.

Example-

This Examples returns the Customer Names from Customer tables whose Names have contains “Anders”

Public void LinqToSqlStringUsingContains()

{

Var q = From c In db.Customers _

Where c.ContactName.Contains("Anders") _

Select c

ObjectDumper.Write(q)

}

 210 views

35⟩ What is Quantifiers in reference linq to Dataset?

Quantifier Operators return the Boolean value (either True or false) if some or all the elements in a sequence satisfy a condition,

Mainly two Quantifiers in linq:

Any

All

Examples (vb)

"Any" to determine if any of the words in the array contain the substring

Public Sub ExampleAny()

Dim words() = {"believe", "relief", "receipt", "field"}

Dim iAfterE = words.Any(Function(w) w.Contains("ei"))

Console.WriteLine("There is a word that contains in the

list that contains 'ei': {0}", iAfterE)

End Sub

Result:

There is a word that contains in the list that contains 'ei': True

"All" to return a grouped a list of products only for categories that have all of their products in stock

Public Sub Quantifier_All_Exam()

Dim products = GetProductList()

Dim productGroups = From p In products _

Group p By p.Category Into Group _

Where (Group.All(Function(p) p.UnitsInStock > 0)) _

Select Category, ProductGroup = Group

ObjectDumper.Write(productGroups, 1)

End Sub

 241 views