Monday, July 30, 2018

Basics of LINQ

//Create a Book 
public class Book
{
        public string Title { get; set; }
        public float Price { get; set; }
}

//Create a repository
public class BookRepository
{
       public IEnumerable<Book> GetBooks()
       {
           return new List<Book>
           {
               new Book(){Title="ADO.Net Step By Step", Price=5},
               new Book(){Title="Asp.Net MVC", Price=9.99f},
               new Book(){Title="Asp.Net WebAPI", Price=7},
               new Book(){Title="C# Advance Topics", Price=12},
               new Book(){Title="C# Advance Topics", Price=12}
 
           };
       }
}

//Use this repository in program class
class Program
{
    static void Main(string[] args)
    {
        var books = new BookRepository().GetBooks();
 
        //LINQ Query Operstors
        /*
        var cheaperBooks = from b in books where b.Price < 10 select b;
        var cheaperBooks = from b in books where b.Price < 10 orderby b.Price  select b;
        */
 
        var cheaperBooks = from b in books
                            where b.Price < 10
                            orderby b.Title
                            select b.Title;
 
        foreach (var book in cheaperBooks)
        {
            Console.WriteLine(book);
            //Console.WriteLine("Books title is {0} and price is {1}", book.Title, book.Price);
        }
 
 
 
 
        //LINQ Extension methods
        /* var cheapBooks = books.Where(b => b.Price < 10);
            var cheapBooks = books.Where(b => b.Price < 10).OrderBy(b => b.Price); */
        var cheapBooks = books
                            .Where(b => b.Price < 10)
                            .OrderBy(b => b.Price)
                            .Select(b => b.Title);
 
        foreach (var book in cheapBooks)
        {
            Console.WriteLine(book);
            //Console.WriteLine("Books title is {0} and price is {1}", book.Title, book.Price);
        }

            //LINQ Extension method 
 
            //In single method lamda condition should always true other wise application will crash
            var book = books.Single(b => b.Title == "ADO.Net Step By Step");
            Console.WriteLine(book.Title);
 
            //In SingleOrDefault method lamda condition is false it will return null or default value
            var book = books.SingleOrDefault(b => b.Title == "ADO.Net Step By Step ++");
            Console.WriteLine(book == null);
 
            var book = books.First(b => b.Title == "C# Advance Topics");
            Console.WriteLine(book.Title + "   " + book.Price);
 
            var book = books.FirstOrDefault(b => b.Title == "C# Advance Topics");
            Console.WriteLine(book.Title + "   " + book.Price);
 
            var book = books.Last(b => b.Title == "C# Advance Topics");
            Console.WriteLine(book.Title + "   " + book.Price);
 
            var book = books.LastOrDefault(b => b.Title == "C# Advance Topics");
            Console.WriteLine(book.Title + "   " + book.Price);
 
            var pageBooks = books.Skip(2).Take(3);
            foreach (var book in pageBooks)
            {
                Console.WriteLine(book.Title);
            }
 
            var count = books.Count();
            Console.WriteLine(count);
 
            var max = books.Max(b => b.Price);
            Console.WriteLine(max);
 
            var minPrice = books.Min(b => b.Price);
            Console.WriteLine(minPrice);
 
            var sum = books.Sum(b => b.Price);
            Console.WriteLine(sum);
 
        Console.ReadKey();
    }
}

Sunday, July 29, 2018

7 Different Star Pattern Programs in C#

https://www.csharpstar.com/star-pattern-programs-in-csharp/


using System;
 
namespace ConsoleApp3
{
 
    class Program
    {
        static void Main(string[] args)
        {
            int numberoflayer = 6, Space, Number;
            Console.WriteLine("Print paramid");
            for (int i = 1; i <= numberoflayer; i++) // Total number of layer for pramid  
            {
                for (Space = 1; Space <= (numberoflayer - i); Space++) // Loop For Space  
                {
                    Console.Write(" ");
                }
 
                for (Number = 1; Number <= i; Number++) //increase the value 
                {
                    Console.Write(Number);
                    // Console.Write("*");
                }
                for (Number = (i - 1); Number >= 1; Number--) //decrease the value  
                {
                    Console.Write(Number);
                    // Console.Write("*");
                }
                Console.WriteLine();
            }
 
            Console.WriteLine("************************");
 
            for (int i = 1; i <= numberoflayer; i++)// Total number of layer for pramid  
            {
                for (Number = 1; Number <= i; Number++)//increase the value  
                {
                    Console.Write(Number);
                    // Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.WriteLine("************************");
 
            for (int i = numberoflayer; i >= 1; i--)// Total number of layer for pramid  
            {
                for (Number = 1; Number <= i; Number++)//increase the value  
                {
                    Console.Write(Number);
                    //Console.Write("*");
                }
                Console.WriteLine();
            }
 
            Console.ReadKey();
        }
    }
}

Thursday, July 26, 2018

Remove duplicates from an array in javascript.

1.)
let a=[7,2,3,2,3,1,2,4,8,5,6,9,5];
let  b=[];
for(let i=0;i<a.length;i++){
    if(b.indexOf(a[i])===-1){
        b.push(a[i]);
    }
}
console.log(b);//[ 7, 2, 3, 1, 4, 8, 5, 6, 9 ]

2.)
let a=[7,2,3,2,3,1,2,4,8,5,6,9,5];
a.sort();
let  b=[];
let _temp;
for(let i=0;i<a.length;i++){
    if(a[i] !== _temp){
        _temp=a[i];
        b.push(a[i]);
    }
}
console.log(b);//[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

3.)
let a=[7,2,3,2,3,1,2,4,8,5,6,9,5];
var obj={};
for(let i of a){
    obj[i]=true;
}
console.log(Object.keys(obj));

4.)
let a=[7,2,3,2,3,1,2,4,8,5,6,9,5];
console.log([... new Set(a)]);
let bSet=new Set(a);
console.log(bSet);

Wednesday, July 25, 2018

getter & setter in Javascript

const person={
    firstName: "Jitendra",
    lastName: "Singh",
    get fullName(){
        return `${person.firstName} ${person.lastName}`;
    },
    set fullName(value){
        const parts=value.split(' ');
        this.firstName=parts[0];
        this.lastName=parts[1];
    }
}

console.log(person.fullName);//Jitendra Singh
person.fullName="Jon Smith";
console.log(person.fullName);//Jon Smith
// console.log(person);/{ firstName: 'Jon',
                        // lastName: 'Smith',
                        // fullName: [Getter/Setter] }

Factory & Constructor Function in javascript.

//Camel Notation :: oneTwoThreeFour
//Pascal Notation :: OneTwoThreeFour

//Factory Function
//use camel notation for construction function it's best practice
function createCircle(radius){
    return {
       /* radius:radius
       draw: function(){
           console.log("draw logic");
       }*/
       radius,
       draw(){
           console.log("draw");
       }
    }
}
var circle1= createCircle(10);
console.log(circle1);

//Constructor Function
//use pascal notation for construction function it's best practice
function CreateCircle(radius){
    this.radius=radius;
    this.draw=function(){
        console.log("draw logic go here !!");
    }
}

const circle1=new CreateCircle(2);
console.log(circle1);

call, apply,bind in javascript

var obj={num:2};
var addThis=function(arg1,arg2,arg3){
    return this.num + arg1+arg2+arg3;
};

//Call
console.log(addThis.call(obj,1,2,3));//8
//apply
var arr=[1,2,3];
console.log(addThis.apply(obj,arr));//8
// bind
var boundaddTo=addThis.bind(obj);
console.log(boundaddTo(1,2,3))//8
OR
var obj={num:2};
var addThis=function(arg1,arg2,arg3){
    console.log(arg1);
    console.log(this.num);
    return this.num + arg1+arg2+arg3;
}.bind(obj,1,2,3)();

What is closure in javascript ?

Closure is nothing but function with preserve data.

//Example 1.

var addTo = function(outer){
     var add=function(inner){
            return outer + inner;
     }
     return add;
}

//console.log(addTo(10)(10));//20

var addOne=addTo(1);
var addTwo=addTo(2);
console.log(addOne(1));//2
console.log(addTwo(2)); //4


Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...