from order in Customer.Orders...
var innerJoinQuery = from cust in customers join dist in distributors on cust.City equals dist.City select new { CustomerName = cust.Name, DistributorName = dist.Name };
from order in Customer.Orders...
var innerJoinQuery = from cust in customers join dist in distributors on cust.City equals dist.City select new { CustomerName = cust.Name, DistributorName = dist.Name };
Single()
/ SingleOrDefault()
|
First
() / FirstOrDefault()
|
Single()
- There is exactly 1 result, an exception is thrown if no result is returned
or more than one result.
SingleOrDefault()
– Same as Single(), but it can handle the null value.
|
First()
- There is at least one result, an exception is thrown if no result is
returned.
FirstOrDefault()
- Same as First(), but not thrown any exception or return null when there is
no result.
|
Single()
asserts that one and only one element exists in the sequence.
|
First()
simply gives you the first one.
|
When
to use
Use
Single / SingleOrDefault() when you sure there is only one record present in
database or you can say if you querying on database with help of primary key
of table.
|
When
to use
Developer
may use First () / FirstOrDefault() anywhere, when they required single
value from collection or database.
|
Single()
or SingleOrDefault() will generate a regular TSQL like "SELECT
...".
|
The
First() or FirstOrDefault() method will generate the TSQL statement like
"SELECT TOP 1..."
|
In
the case of First / FirstOrDefault, only one row is retrieved from the
database so it performs slightly better than single / SingleOrDefault. such a
small difference is hardly noticeable but when table contain large number of
column and row, at this time performance is noticeable.
|
public static string RemoveAllNamespaces(string xmlDocument)
{
var xml = XElement.Parse(xmlDocument);
xml.Descendants().Select(o => o.Name = o.Name.LocalName).ToArray();
return xml.ToString();
}
A simple select with where is classified under Restriction Operators in LINQ. Below is a sample that uses where to find all elements of an array less than 5 using LINQ Select statement
public void Linq() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Console.WriteLine("Numbers < 5:"); foreach (var x in lowNums) { Console.WriteLine(x); } }Result
Numbers < 5: 4 1 3 2 0
A simple select is classified under Projection Operators in LINQ. Below is a sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array using LINQ Select statement
public void Linq() { string[] words = { "sUmMeR", "wInTeR", "aUtUmN" }; var upperLowerWords = from w in words select new { Upper = w.ToUpper(), Lower = w.ToLower() }; foreach (var ul in upperLowerWords) { Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower); } }Result
Uppercase: SUMMER, Lowercase: summer Uppercase: WINTER, Lowercase: winter Uppercase: AUTUMN, Lowercase: autumn
Below is a sample to select to produce a sequence containing text representations of digits and whether their length is even or odd using LINQ Select statement
public void Linq() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var digitOddEvens = from n in numbers select new { Digit = strings[n], Even = (n % 2 == 0) }; foreach (var d in digitOddEvens) { Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd"); } }Result
The digit five is odd. The digit four is even. The digit one is odd. The digit three is odd. The digit nine is odd. The digit eight is even. The digit six is even. The digit seven is odd. The digit two is even. The digit zero is even.
A simple select is classified under Projection Operators in LINQ. Below is a sample that just selects the array of numbers from an array using LINQ Select statement
public void Linq() { int[] numbers = { 1, 2, 3 }; var selctedNumbers = from n in numbers select n; Console.WriteLine("Selected Numbers:"); foreach (var i in selctedNumbers) { Console.WriteLine(i); } }Result
Numbers: 1 2 3
Below is a sample to select to return a sequence of just the names of a list of Employees using LINQ Select statement
public void Linq() { ListResultEmployees = GetEmployeeList(); var employeeNames = from e in Employees select e.EmployeeName; Console.WriteLine("Employee Names:"); foreach (var employeeName in employeeNames) { Console.WriteLine(employeeName); } }
Employee Names: David Ajith Susan