WHAT'S NEW?
Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

IEnumerable 


  • Is for LINQ to Objects
  • Objects that are in memory on Heap


IQueryable 


  • Is for working on LINQ to SQL
  • Inside it, it using Expressions to frame and execute sql queries.


Func<T>

  • Can return a value. The last value in the definition is the return.
Ex: 

Func<int,int> PrintNumber = x => (x);
            Console.WriteLine(PrintNumber(50));


Action<T>

  • Cannot return any value. It is only to feed and use directly

Ex:
Action PrintEmpty = () => Console.WriteLine("Hi");
            PrintEmpty();

            Action<int> PrintOneNumber = x => Console.WriteLine(x);
            PrintOneNumber(5);

Expression:

  • To be used to help the developer better understand the code.
  • It is slightly different from Func<T> and Action<T>.
  • Expression takes Func<T> as input and is to be used as a variable and not as a method that is the case with Func or Action

Ex:
Expression<Func<int, int, int>> PrintMulti = (x, y) => (x * y);

            Func<int, int, int> Mutli = PrintMulti.Compile();
            var zExp = Mutli(5, 10);
            Console.WriteLine(z);




A simple example for converting the normal linq query to a Parallel Linq query in dotnet.


public void NormalLinqQuery()
        {
            IEnumerable<int> localRange = Enumerable.Range(1, 100000000);
            Stopwatch watch = Stopwatch.StartNew();
            var output = localRange
                .Where(getNum => getNum % 1234567 == 0)
                .Select(rangeNum => rangeNum);
            foreach (var num in output)
                Console.WriteLine(num);
            Console.WriteLine("Normal Query in millisec: {0}",watch.ElapsedMilliseconds);
        }

        public void ParallelLinqQueryExample()
        {
            IEnumerable<int> localRange = Enumerable.Range(1, 100000000);
            Stopwatch watch = Stopwatch.StartNew();
            var output = localRange
                .AsParallel()
                .Where(getNum => getNum % 1234567 == 0)
                .Select(rangeNum => rangeNum);
            foreach (var num in output)
                Console.WriteLine(num);
            Console.WriteLine("Parallel Query in millisec: {0}", watch.ElapsedMilliseconds);
        }