Friday, September 29, 2017

C# IEnumerable vs IQueryable

The primary difference is that IEnumerable is great for working with in-memory collections, and IQueryable for a remote data source, like a database or web service.

IEnumerable : 
IEnumerable represent a forward collection of T and It has one GetEnumerater() method, from which you can iterate through collection.
  • It does not support adding, removing objects on collection.
  • It loads all object collection into local memory by using LINQ or entity framework before running query.
  • It is best option for in-memory data source like object collections, if your IEnumerable query is being executed against external database, it would be network overhead and first loads whole entity data in local memory and then apply filter.
 public interface IEnumerable<out T> : IEnumerable
    {      
        IEnumerator<T> GetEnumerator();

    }


IQueryable
 IQueryable is similar like LINQ and it parse the expression tree and generates SQL querya nd execute the script against the external data source.
  • It inherits the IEnumerable interface so it allows you to iterate over collection using ForEach statement.
  • It is best option for out-memory data source like database and it send only the filtered data to client and reduce network overhead due to reducing record count.

public interface IQueryable<out T> : IEnumerable<T>
{

}

Tuesday, September 5, 2017

Difference between Select and SelectMany in LINQ

Select and SelectMany are projection operator and Select method is used to select a
value from collection and SelectMany method is used to select value from multiple
collection. 

In below example, we will see that Select will returns the collection of Player object and to read Player Name, we need one more For Each loop to read the Player object and
SelectMany will flatten the list into a single list of Players, so we need only one for-each
loop to get player name.

Select Vs Select Many


C# Example : 


List<Team> teams = new List<Team>();


Team team1 = new Team {
             Name = "USA",
             Players = new List<Player> {
                                     new Player {  PlayerName = "John" },
                                     new Player { PlayerName = "Peter" },
                                     new Player { PlayerName = "Steve" }
             } };


Team team2 = new Team
         {
             Name = "AUS",
             Players = new List<Player> {
                    new Player {  PlayerName = "Ricky" },
                    new Player { PlayerName = "McGraw" },
                    new Player { PlayerName = "Donald" }
             }
         };


teams.Add(team1);
teams.Add(team2);


Select : 
by using Select query. We need two for-each loop to retrieve the player name from a
team collection of arrays, because Select returns a collection of arrays.


C# Example : 


var resultSelect = teams.Select(e => e.Players);


foreach (var playerList in resultSelect)
         {
                foreach (var player in playerList)
             {                      Console.WriteLine(player.PlayerName);
             }
                Console.WriteLine();
         }


Output:


John
Peter
Steve


Ricky
McGraw
Donald


SelectMany: 
By using SelectMany query, we only need one for each loop to retrieve the player name
from team collection of array and SelectMany returns a one dimensional collection.

C# Example : 
     
      var resultSelectMany = teams.SelectMany(emp => emp.Players);


      foreach (var player in resultSelectMany)
         {
                Console.WriteLine(player.PlayerName);
         }


            Console.ReadKey();
     }


Output:
John
Peter
Steve
Ricky
McGraw
Donald


Thanks for visiting !!

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...