Monday, November 2, 2015

row_number() in lambda experssion

Hi friends,

Today I am going to show how row_number() implement in lambda expression.

Suppose a list of customer say List<Customer>. Customer have CustomerId, LoginDate,....

If you want to know the latest login date of each customer.


var customerLoginList = customers.OrderByDescending(x => x.LoginDate).GroupBy(x => x.CustomerId).
                           Select(g => new { g, count = g.Count() }).
                           SelectMany(t => t.g.Select(b => b).
                           Zip(Enumerable.Range(1, t.count), (j, i) => new { j.CustomerId, j.LoginDate, rn = i }));


From the above code you will get list which contains customerid with row number. Now you just filter the list where rn = 1.

Sunday, June 14, 2015

Calculate date difference between two rows of the same table

Hi friends,

Today I am going to demonstrate calculate date difference between two rows of the same table.

Consider the below table.




The query for calculating date difference between 2nd row to 1st row, 3rd row to 2nd row and so on.

Calculate difference in days:

DECLARE @temp1 TABLE (
 StudentID INT
 ,NAME VARCHAR(50)
 ,RN INT
 ,createddate DATETIME
 )

INSERT INTO @temp1 (
  StudentID
 ,NAME
 ,RN
 ,createddate
 )
SELECT [StudentID]
 ,dbo.Student.NAME
 ,RN = ROW_NUMBER() OVER (
  PARTITION BY [StudentID] ORDER BY createddate
  )
 ,createddate
FROM dbo.Student

DECLARE @temp2 TABLE (
 ID INT
 ,StudentID INT
 ,olddate DATETIME
 ,newdate DATETIME
 ,[Difference_InDays] INT
 );

;WITH cte as(
SELECT ROW_NUMBER() OVER(ORDER BY c1.StudentID) AS ID ,c1.StudentID,
       olddate=CASE WHEN c1.createddate=c2.createddate THEN NULL ELSE C1.createddate END,
       newdate=CASE WHEN c1.createddate=c2.createddate THEN NULL ELSE C2.createddate END
FROM @temp1 c1
INNER JOIN @temp1 c2 ON c1.[StudentID] = c2.[StudentID]
 AND c2.RN = c1.RN + 1
 )
INSERT INTO @temp2 (
  ID
 ,StudentID
 ,olddate
 ,newdate
 ,[Difference_InDays]
 )
SELECT *
 ,DATEDIFF(DD, c.olddate, c.newdate) AS [Difference_InDays]
FROM cte c

SELECT *
FROM @temp2 t 

Result:




Calcualte difference in months:

DECLARE @temp1 TABLE (
 StudentID INT
 ,NAME VARCHAR(50)
 ,RN INT
 ,createddate DATETIME
 )

INSERT INTO @temp1 (
  StudentID
 ,NAME
 ,RN
 ,createddate
 )
SELECT [StudentID]
 ,dbo.Student.NAME
 ,RN = ROW_NUMBER() OVER (
  PARTITION BY [StudentID] ORDER BY createddate
  )
 ,createddate
FROM dbo.Student

DECLARE @temp2 TABLE (
 ID INT
 ,StudentID INT
 ,olddate DATETIME
 ,newdate DATETIME
 ,[Difference_InMonth] INT
 );

;WITH cte as(
SELECT ROW_NUMBER() OVER(ORDER BY c1.StudentID) AS ID ,c1.StudentID,
       olddate=CASE WHEN c1.createddate=c2.createddate THEN NULL ELSE C1.createddate END,
       newdate=CASE WHEN c1.createddate=c2.createddate THEN NULL ELSE C2.createddate END
FROM @temp1 c1
INNER JOIN @temp1 c2 ON c1.[StudentID] = c2.[StudentID]
 AND c2.RN = c1.RN + 1
 )
INSERT INTO @temp2 (
  ID
 ,StudentID
 ,olddate
 ,newdate
 ,[Difference_InMonth]
 )
SELECT *
 ,DATEDIFF(MM, c.olddate, c.newdate) AS [Difference_InMonth]
FROM cte c

SELECT *
FROM @temp2 t


Result:



Wednesday, February 25, 2015

Validation Group in MVC

Hi friends,

Today I am going to demonstrate how to implement validation group in MVC.

For this you have to download one validation group jquery and and one validation group dll
Suppose we have customer model, in which we have Name and Email property. We validate this two property on two different buttons.

Step 1 : Add McjDevelopment.Mvc3ValidationGroups.dll in your project.

Step 2 : Add jquery.validate.unobtrusive.validationgroups.js in your project.

Step 3 : Create Customer Model

 public class Customer
    {
        [Required]
        [ValidationGroup("Submit1")]
        public string Name { getset; }
 
        [Required]
        [ValidationGroup("Submit2")]
        public string Email { getset; }
    }

Step 4 : Update your view to use ‘jquery.validate.unobtrusive.validationgroups.js’ immediately after ‘jquery.validate.unobtrusive.js’ (or ‘jquery.validate.unobtrusive.min.js’)








Step 5 : Update your view to include where you want each groups Validation to appear
















and don't forget to add "data-val-valgroup-name" tag at you buttons.

Results : When you click Submit1 button














When you click on Submit2 button













McjDevelopment.Mvc3ValidationGroups.Release.0.3.0.0.zip


Monday, February 9, 2015

How to ignore a certificate error - without the certificate



Hi Friends,

If you getting error like "Could not establish trust relationship for the SSL/TLS secure channel".

Do following two things

1) Add using System.Net; at your class

2) Add below line before calling your service method
 
ServicePointManager.ServerCertificateValidationCallback += 
(sender, cert, chain, sslPolicyErrors) => true;
 
 
The above line returning true will allow ignoring the validation error.