How a Foreign Key is Represented in LINQ ? | Foreign Key in LINQ Join | LINQ Interview Question | LINQ Programmer Guide

, ,

In LINQ you do not have to use join as often as you do in SQL because foreign keys in LINQ are represented in the object model as properties that hold a collection of items. For example, a Customer object contains a collection of Order objects. Rather than performing a join, you access the orders by using dot notation:
from order in Customer.Orders...
Join operations create associations between sequences that are not explicitly modeled in the data sources. For example you can perform a join to find all the customers and distributors who have the same location. In LINQ the join clause always works against object collections instead of database tables directly.

var innerJoinQuery =
    from cust in customers
    join dist in distributors on cust.City equals dist.City
    select new { CustomerName = cust.Name, DistributorName = dist.Name };

What is the difference between FirstOrDefault and SingleOrDefault inlinq? | Linq Interview Question | ASP.NET Programmer Guide

,

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.

Removing All Namespaces from XML Using C# Linq | Linq Programmer Guide

,
Below is the simple C# linq code snippet that strips namespaces from an XML string.

public static string RemoveAllNamespaces(string xmlDocument)
{
    var xml = XElement.Parse(xmlDocument);
    xml.Descendants().Select(o => o.Name = o.Name.LocalName).ToArray();
    return xml.ToString();
}

Using LINQ With C# 5.0 In Visual Studio 2012 | LINQ Tutorial | C# Tutorial

,
Writing software means that you need to have a database sitting at the back end, and most of the time goes into writing queries to retrieve and manipulate data. Whenever someone talks about data, we tend to only think of the information that is contained in a relational database or in an XML document.

The kind of data access that we had prior to the release of .NET 3.5 was only meant for or limited to accessing data that resides in traditional data sources as the two just mentioned. But with the release of .NET 3.5 and higher versions like .NET 4.0 and .NET 4.5, that has Language INtegrated Query (LINQ) incorporated into it, it is now possible to deal with data residing beyond the traditional homes of information storage. For instance, you can query a generic List type containing a few hundred integer values and write a LINQ expression to retrieve the subset that meets your criterion, for example, either even or odd.

How to do a Select with Where using LINQ in C#?

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

How to do a Select with Anonymous Types using LINQ in C#?

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.

How to do a Simple Select using LINQ in C#?

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() 
{ 
    List Employees = GetEmployeeList(); 
  
    var employeeNames = 
        from e in Employees 
        select e.EmployeeName; 
  
    Console.WriteLine("Employee Names:"); 
    foreach (var employeeName in employeeNames) 
    { 
        Console.WriteLine(employeeName); 
    } 
}
Result
Employee Names:
David
Ajith
Susan