Tuesday, 21 April 2015

How to post json array object angularjs

var helloAjaxApp = angular.module("helloAjaxApp", []);

helloAjaxApp.controller("CompaniesCtrl", ['$scope', '$http', function($scope, $http) {
$scope.companies = [
                   { 'name':'Infosys Technologies',
                    'employees': 125000,
                    'headoffice': 'Bangalore'},
                    { 'name':'Cognizant Technologies',
                    'employees': 100000,
                    'headoffice': 'Bangalore'},
                    { 'name':'Wipro',
                    'employees': 115000,
                    'headoffice': 'Bangalore'},
                    { 'name':'Tata Consultancy Services (TCS)',
                    'employees': 150000,
                    'headoffice': 'Bangalore'},                    
                   ];

$scope.addRowAsyncAsJSON = function(){
$scope.companies.push({ 'name':$scope.name, 'employees': $scope.employees, 'headoffice':$scope.headoffice });
// Writing it to the server
//
var dataObj = {
name : $scope.name,
employees : $scope.employees,
headoffice : $scope.headoffice
};
var res = $http.post('/savecompany_json', dataObj);
res.success(function(data, status, headers, config) {
$scope.message = data;
});
res.error(function(data, status, headers, config) {
alert( "failure message: " + JSON.stringify({data: data}));
});
// Making the fields empty
//
$scope.name='';
$scope.employees='';
$scope.headoffice='';
};
}]);

Monday, 20 April 2015

Find the count of a weekday between two dates

Single line in Linq This gets you the number of weekdays, i.e., Monday to Friday between given two dates.

DateTime dt1 = new DateTime(2011, 1, 1);
DateTime dt2 = new DateTime(2011, 1, 15);

int days = (from d in (Enumerable.Range(0, 1 + dt2.Subtract(dt1).Days).Select(offset => dt1.AddDays(offset)).ToArray())where d.DayOfWeek >= DayOfWeek.Monday & d.DayOfWeek <= DayOfWeek.Friday1).Sum;



You can easily modify it to get the count of a particular day as,


DateTime dt1 = new DateTime(2011, 1, 1);
DateTime dt2 = new DateTime(2011, 1, 15);

int days = (from d in (Enumerable.Range(0, 1 + dt2.Subtract(dt1).Days).Select(offset => dt1.AddDays(offset)).ToArray())where d.DayOfWeek == DayOfWeek.Monday).Sum;