Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How to read xml document by using linq

Answer»

In Below example we will read xml document in c# using linq. First i have created a xml file with below content


< ?xml version="1.0" encoding="utf-8" ?>
< websites xmlns="URN:websitename">
< websitename>getproductprice.com< /websitename>
< websitename>interviewqsn.com< /websitename>
< /websites>


To read above xml file we will write below code and name of ABOV file is websites.xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace LinqExample
{
class websitename
{
STATIC void Main(string[] args)
{
var doc = XDocument.Load("websites.xml");
var QUERY = from x in doc.Descendants("{urn:websitename}websitename")
select x;
foreach (var item in query)
{
Console.WriteLine(item);
}
}
}
}

2.

Code to use LINQ on datatable

Answer»

Below code will helps you to understand that how linq is apply to datatable in asp.net:-
using LINQ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace LinQSamples
{
class Program
{
static void Main(string[] args)
{
DtMethod();
}
public static DataTable createDataTable()
{
DataTable DT = new DataTable();
dt.Columns.Add("EMPID", typeof(INT));
dt.Columns.Add("empname", typeof(string));
dt.Columns.Add("salary", typeof(int));
dt.Rows.Add(1, "Adam", 10000);
dt.Rows.Add(2, "Sutro", 1000);
dt.Rows.Add(3, "Ricky", 3000);
dt.Rows.Add(4, "Martin", 66000);
dt.Rows.Add(5, "Mike", 5000);
dt.Rows.Add(6, "Jonson", 100);
dt.Rows.Add(7, "Michel", 4500);
dt.Rows.Add(8, "John", 7000);
return dt;
}
public static void DtMethod()
{
DataTable dtTable = createDataTable();
//SELECT Statement using LINQ is given below
var SelectQuery = from SQ in dtTable.AsEnumerable() select sq;
console.WriteLine(" nResult for select statement");
Console.WriteLine("-------------------");
foreach (DataRow Query in SelectQuery)
{
Console.WriteLine("{0} t{1} t{2}", Query.ItemArray);
}
///Below Statement is with where condition(empid =100)
var querySalarySum = from qs in dtTable.AsEnumerable()
where qs.Field("empid") == 100
select qs;
Console.WriteLine("EmpId t Name t t Salary");
foreach (DataRow dr in querySalarySum)
{
Console.WriteLine("{0} t{1} t{2}",
dr.Field("empid"),
dr.Field("empname"),
dr.Field("salary"));
}
}
}
}

3.

Syntax to sort string Array using LINQ

Answer»

Below code is USED to sort string ARRAY using LINQ:-
static void Main(string[] args)
{
string[] data = { "Adam", "Sutro", "Lilly" };
List sortData = NEW List(data);
sortData.Sort((string a, string b) =&GT;
{
return a.CompareTo(b);
}
);
Console.WriteLine(string.Join(",", sortData.ToArray()));
Console.Read();
}

4.

How to sort array using LINQ

Answer»

Below program first create INT array and then apply QUERY on that array and sort with the help of linq
using System;
using System.Linq;
class SortArrayLinqProgram
{
STATIC void Main()
{
int[] array1 = { 5, 4, 1, 2, 3 };
var query = from element in array1
orderby element
select element;
int[] array2 = query.ToArray();
foreach (int value in array2)
{
Console.WriteLine(value);
}
}
}

5.

Get multiple of 3 from given array with LINQ

Answer»

Below code helps US to get element in array is multiple of 3 or not if it is value return is true else false

static void Main(STRING[] args)
{
//below is the array contains elements
int[] arrayno = { 12, 33, 24, 90, 99, 42, 75 };
BOOL CheckMultiple = CheckElement(arrayno, 3);
}

//function which return either true or false
private static bool CheckElement(int[] arrayno, int divisor)
{
bool CheckMultiple = arrayno.All(number => number % divisor == 0);
return CheckMultiple;
}

6.

Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query

Answer»

Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query
Below are the 3 MAIN difference between Single() vs SingleOrDefault() vs First() vs FirstOrDefault()

Defination


(1)Single():- This will return a single specific ELEMENT froma sequence
(2)SingleOrDefault():-This will return the single specific element from sequence or will return default value if that element not found
(3)First():-This will return the first element from sequence
(4)FirstOrDefault():- This will returns the first element of a sequence or default value if no element is found

Exception thrown


(1)Single():-It will THROWS error when 0 or more then 1 elements comes in result
(2)SingleOrDefault():-It will throws error if more then 1 element in result
(3)First():-This will throws error if no elements in the result is return
(4)FirstOrDefault():-If source is null then it will throws error

When to Use


(1)Single():-It will be USED when we EXACTLY have 1 element expected and value is not 0 or more then 1
(2)SingleOrDefault():-When we expect 0 or 1 element then we use it
(3)First():-When we expect more then 1 element and we need only first value
(4)FirstOrDefault():-When more then 1 element expected and we need only the first element. It is also okay when result is empty
7.

What are the different LINQ provide

Answer» LINQ change aspects how to handle DATA in our applications. Below are the list of different LINQ provider THREE of these are very common while using linq
(1)LINQ to objects
(2)LINQ to XML
(3)LINQ to SQL

linq providers
8.

How to do Array Slicing Using LINQ?

Answer»

How to do ARRAY SLICING Using LINQ?
To understand this we will take an array of 10 elements and out of which we need to return FIVE of them, with the intention to return the rest of them on demand. And it is known as pagination and it is achieved by using two mainly methods of LINQ that are Skip() and Take(). So import LINQ and use two of these methods as in below given example:-


var objElements=new string[] {"E1","E2","E3","E4","E5","E6","E7","E8","E9","E10"};
var objSliced=objElements.Skip(0).Take(5);
foreach (var obj in objSliced)
Console.WriteLine(obj);



In above CODE we PASS number of element we want to skip is pass to Skip() method. And this will turns out to be starting index as we put 0 with Skip methods. And in second method is Take() here we pass the number of elements which we want to include in Take() method. And this operation returns a new IEnumerable that we can enumerate.

9.

Important aspects of Query Operators in LINQ?

Answer»

Important aspects of Query Operators in LINQ?
There are lots of query-patterns which explain how should write query on data by using LINQ. And these patterns are desgined by help of extension methods. And these query patterns are KNOWN as Query Operators. LINQ query operators can be categorized into below ways.
(1)Join Operators:-Join, GroupJoin
(2)Projection Operations:-Select
(3)Sorting Operators:-OrderBy, OrderByDescending, ThenBy, ThenByDescending, REVERSE
(4)Grouping Operators:-GroupBy, ToLookUp
(5)Filtering Operators:-OfType, Where
(6)Aggregation:-Aggregate, AVERAGE, Count, LonCount, Max, Min, Sum
(7)Set Operators:-Distinct,Except,Intersect,Union
(8)Conversions:-AsEnumerable, AsQueryable, Cast, OfType, TOARRAY, ToDictionary, ToList,ToLookup
(9)Partition Operators:-Skip, SkipWhile, Take, TakeWhile