Var vs IEnumerable when retrieving LINQ Query

No.of Views2319
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Jan 2011 02:01:53
Tag : LINQ , General
Var derives type from the right hand side. And its scope is in the method. And it is strongly typed. IEnumerable is interface which allows forward movement in the collection.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article I will show, what is use of Var and IEnumerable when retrieve data with LINQ Query.Var derives type from the right hand side. And its scope is in the method. And it is strongly typed. IEnumerable is interface which allows forward movement in the collection.

Implementation

We have seen normally 

Image Loading

Or 

Image Loading

Now question is where to use var or IEnumerable? Let us first have a look on both keywords.Var derives type from the right hand side. And its scope is in the method. And it is strongly typed. IEnumerable is interface which allows forward movement in the collection.

LINQ where to use what ?

If we really do not have idea that what would be the type of query then we will be using var. and if we are sure about the type of output then we would be using IEnumerable .

Using IEnumerable <T>

In the below example, we are querying in collection of string so we know the output would be collection of string, so we are using here IEnumerable with string.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lstStrings = new List<string> {"Dhananjay Kumar","Mahesh Chand","John papa","Mike Gold","Shiv Prasad Koirala","Victor ","Pinal Dave" };
            IEnumerable<string> result = from r in lstStrings select r;
            foreach (var r in result)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
        }
    }
}

Output 

Image Loading

Using Var

Now if we don’t know exactly what would be the type of result in that case we will be using var. There might be some scenario where you want to return anonymous type from the query

Image Loading

If you see the above query we do not know the type of result.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            var result = from r in context.Persons select new { NameOfperson = r.FirstName + r.LastName };
            foreach (var r in result)
            {
                Console.WriteLine(r.NameOfperson);
            }
            Console.ReadKey(true);
        }
    }
}

Output 

Image Loading

That's all, hopes help and thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    In this article we will focus LINQ Remote and Local exaction such as This query executes on the server, Remote execution of query is default in LINQ, In Remote execution advantage of Databases index can be taken, Remote execution allows us to take the advantage of Database server engine, Remote execution allows us to only select particular rows from the table. This is very useful when we do have large amount of data in the server.
    Published Date : 07/Jan/2011
    In this article, we will see how to work with LINQ against IIS.
    Published Date : 10/Aug/2010
    how to create a XML tree using Functional Construction method of LINQ to XML.
    Published Date : 13/Apr/2010
    This article demonstrates how to use LINQ on DataTable, XML Data using LINQ to XML and SQL server data base using LINQ to SQL Classes.
    Published Date : 28/Jun/2010
    In this article we will focus, how to read CSV file using LINQ to CSVProvider.I hope we know read CSV file using StreamReader or any other readers in C#. But when we are use the LINQ to CSVProvider, and then we could get additional features to query data in LINQ as like Sql Query.
    Published Date : 01/Jan/2011
Comments
By:darshanDate Of Posted:7/21/2011 2:26:02 AM
IEnumarable and IEnumerator
can you please explain some difference for this ? thank you
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</