Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Tutorial On LinQ and Simple LinQ examples

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 468
    Comment on it

    Introduction of LINQ:

    Abbreviated for Language-Integrated Query (LINQ). It was introduced in Visual Studio 2008 and .NET Framework version 3.5. The main concept behind it was to fill the gap between world of objects and the world of data. LINQ makes a query first-class language construct in C#. The data to be queried can take the form of XML (LINQ to XML), databases (LINQ-enabled ADO.NET: LINQ to SQL, LINQ to Dataset and LINQ to Entities) and objects (LINQ to Objects). LINQ is also highly extensible and allows you to build custom LINQ enabled data providers (e.g.: LINQ to Amazon, LINQ to NHibernate, LINQ to LDAP).

    Need & Advantage of LINQ:

    1. Syntax error for SQL queries are not detected untill runtime, whereas LINQ is integrated into the language.
    2. XML parsing, iterating, and manipulation is quite tedious, whereas LINQ to XML provides a more powerful and easier-to-use interface for working with XML data, as though it were a database.
    3. LINQ is not only for querying data but for formatting, validating, and even getting data into the necessary format.

    LINQ Syntax:

    1. Query Expression

    2. from int in integers where int>5 select int;
    3. Lambda Expression

      integerlist.Where(i => i>5).select(i=>i);

    Types of LINQ:

    1. LINQ to Objects
    2. It enables you to write queries against any collection type that implements an interface called IEnumerable.
    3. LINQ to XML
    4. It enables you to create, read and write XML based Data.
    5. LINQ to DataSet
    6. It enables you to write queries against DataSet.
    7. LINQ to SQL
    8. It provides a run-time infrastructure for managing relational data as objects without losing the ability to query. With the help of which your application is free to manipulate the objects while LINQ to SQL stays in the background tracking your changes automatically.
    9. LINQ to Entities
    10. It enables you to write queries against the entity framework conceptual model. They are represented by command tree queries, which execute against the object context.

    Detail of all above types will be discussed in my next blog.

    In this Blog we will discuss Var, IEnumerable and LAMBDA Expressions used with LINQ.

    Var Example:

    Since Var is anonymous types, hence we can use it whenever we don't know the type of output or it is anonymous. For example:

    var q = (from e in Employee join d in Department on e.DeptID equals d.DeptID select new {e.EmpID, e.Name=e.Firstname+" "+e.LastName, d.DeptName});
    

    IEnumerable Example:

    IEnumerable<Employee> lst = (from e in Employee where e.City=="DehraDun" select e);
    

    LAMBDA Expression

    (Input Parameters) => Method Expression Assume we have an array of integers and we want to get count of integers greater than 10.

    int[] intarray = {2,6,10,44,56,9,3,30};
    int count = intarray.count(x => x>10);
    

    If we have list of Users and want to get all active users: List AllUsers = New List; List ActiveUsers = AllUsers.Where(u => u.Active == True);

    Will discuss more in deep in my upcoming Blog. So, till then "Happy Coding"

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: