Shailendra Saxena's Technical Blog

Sequence contains no elements Linq

clock February 20, 2014 02:44 by author ShailendraSaxena


" Sequence contains no elements"




Error : While executing Linq Query error came as Sequence contains no elements.


Resolution: This error comes when you execute .First or .Single with LINQ Query because when no record is found then it throws exception.


So it is better to use . FirstOrDefault because in this case either record will be returned or null will be return.


LINQ -Querying

clock December 16, 2012 01:18 by author ShailendraSaxena



For Linq Queries you must have a source defined before we write a query against it. LINQ can query nearly all collections of data in .net Framework.

For Practicing LINQ Queries LinqPad is a great tool. You can download it from


//Data in Array 


 string[] employees = {"Shailendra","Sachin","Rohit","Chawla"};


 var empQuery =from emp in employees

 where emp.Length>5

 select emp;



 //Order By Clause

 var empQuery =from emp in Employees

 where emp.Country=="USA"

 orderby emp.EmployeeID

 select emp;




  //Group By Clause

 var empQuery =from emp in Employees

 group emp by emp.Title;




   //Join Query

var empQuery =from emp in Employees

join et in EmployeeTerritories on emp.EmployeeID equals et.EmployeeID

select  new { EmployeeId=emp.EmployeeID,EmployeeName=emp.FirstName + "" + emp.LastName};


LINQ Basics

clock July 31, 2012 12:00 by author ShailendraSaxena


Language-Integrated Query (LINQ)
is a new feature introduced in visual studio 2008 and .NET Framework 3.5. Developers can write the queries in the code to retrieve the data from various types of data sources like SQL, XML and XQuery.

Earlier developers have to learn the syntax for writing queries for each data source. LINQ simplifies by offering a model to work with the data across various kinds of data sources.

LINQ Architecture


LINQ Flavours

When generating database mappings using LINQ to SQL, which tool allows you to create entity classes using a convenient graphical interface?
The Object Relational Designer (O/R Designer) provides a visual design surface for creating LINQ to SQL entity classes and associations (relationships) that are based on objects in a database
Below is the three advantages of LINQ over stored procedures.

Debugging - As debug point concern, as LINQ is part of .NET, we can use the visual studio's debugger to debug the queries but it is tough to debug the Stored procedure as it will not support the visual studio debugger.

Deployment - In case of deployment, we need to provide an additional script for stored procedures to execute but in case of LINQ, it will complie into single DLL hence deployment becomes easier.

Type Safety - As LINQ is type safe, the queries errors are type checked at compile time. Better suggest to use LINQ because it helps to encounter an error at the compile time rather than at runtime exception.
 The disadvantage with LINQ is, it is not a precompiled statement whereas stored procedures are precompiled. In case of LINQ the queries needs to be compile before the execution. So according to this, I can say stored procedures are faster in performance as compared to LINQ.
What are the advantages and disadvantages of LINQ.
Adv. of LINQ:
  • Supports type safety
  • Supports abstraction and hence allows developers to extend features such as multi-threading.
  • Easier to deploy
  • Simpler and easier to learn
  • Allows for debugging through .NET debugger.
  • Support for multiple databases
Disad. of LINQ:
  • LINQ needs to process the complete query, which might have a performance impact in case of complex queries
  • LINQ is generic, whereas stored procedures etc can take full advantage of database features.
  • If there has been a change, the assembly needs to be recompiled and redeployed. 

Can   LINQ  be used with databases other than SQL Server?   

LINQ supports Objects, XML, SQL, Datasets and entities. One can use LINQ with other databases through LINQ to Objects or LINQ to Datasets, where the objects and datasets then take care of database specific operations and LINQ only needs to deal with those objects, not the database operations directly. 
What are the three main components of LINQ or Language Integrated Query?
1. Standard Query Operators
2. Language Extensions
3. LINQ Provider
How are Standard Query Operators implemented in LINQ?
Standard Query Operators are implemented as extension methods in .NET Framework. These Standard Query Operators can be used to work with any collection of objects that implements the IEnumerable interface. A class that inherits from the IEnumerable interface must provide an enumerator for iterating over a collection of a specific type. All arrays implement IEnumerable. Also, most of the generic collection classes implement IEnumerable interface.
What are the steps to use LINQ to SQL in .Net Project.

1.      Create a connection to a database

1.       Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.
2.       Add LINQ to SQL file.
1.       The Object Relational Designer (O/R Designer) is opened for the DBNAME.dbml file.
3.       To add Table to OR Designer.
1.       Select the table from the data connection and drag it to left pane of OR Designer.
4.       Save the Project
5.       Once you save the project DataContext object for the project. DBNAMEDATACONTEXT gets created.
Following is the code in vb to fill a datagridview.
Dim db As New northwindDataContext
Dim londonCusts = From cust In db.Customers _
                  Where cust.City = "London" _
                  Select cust
DataGridView1.DataSource = londonCusts




<<  June 2017  >>






Flag Counter


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign In