Returning Only a Subset of Members

Often you’ll need to return only a subset of the properties of a class; this exercise shows how to do that. Take the case of the Employeeclass shown here-

The Employee Class with Five Properties
public class Employee
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public decimal Compensation { get; set; }
     public int Department { get; set; }
}

public HttpResponseMessage Get()
{
     var values = list.Select(e => new
                         
                              Identifier = e.Id, 
                              Name = e.FirstName + ” ” + e.LastName 
                         });
     var response = new HttpResponseMessage(HttpStatusCode.OK)
                         {
                              Content = new ObjectContent(values.GetType(),
                                                       values,
                                                       Configuration.Formatters.JsonFormatter)
                         };
     return response;
}

This code explicitly returns ObjectContent from the Getaction method, using the anonymous type that we create in the select clause. The important point to note here is that XmlFormattercannot handle anonymous types. 
We pass the JsonFormatterwhile creating the ObjectContentinstance and make sure conneg result is not used and any formatter other than JsonFormatteris not picked for serialization. Here is the JSON output:
[
     {
          “Identifier”: 12345,
          “Name”: “John Human”
     },
     {
          “Identifier”: 12346,
          “Name”: “Jane Public”
     },
     {
          “Identifier”: 12347,
          “Name”: “Joseph Law”
     }
]

A List of Common HTTP Status Codes

200 All is good; response will include applicable resource information, as well.

201 Resource created; will include the Location header specifying a URI to the newly created resource.

202 Same as 200, but used for async; in other words, all is good, but we need to poll the service to find out when completed.

301 The resource was moved; should include URI to new location.

400 Bad request; caller should reformat the request.

401 Unauthorized; should respond with an authentication challenge, to let the caller resubmit with appropriate credentials.

403 Access denied; user successfully authenticated, but is not allowed to access the requested resource.

404 Resource not found, or, caller not allowed to access the resource and we don’t want to reveal the reason.

409 Conflict; used as a response to a PUT request when another caller has dirtied the resource.

500 Server error; something bad happened, and server might include some indication of the underlying problem.

JavaScript Array Manipulation

To remove an element of an array at an index i:
array.splice(index, 1);

If you just want to make the element at index i no longer exist, but you don’t want the indexes of the other elements to change:

delete array[index];

And if you need the value of that item, you can just store the return value:

var value = array.splice( index, 1 );

—————

Programming Vocabulary

Properties are values associated with objects.
Methods are actions objects can perform.

Function Parameters and Arguments

When you call a function, you can pass values to it. These values are called arguments or parameters.
Identifiers, in the function definition, are called parameters.
Multiple parameters are separated by commas:
function myFunction(parameter1, parameter2) {
    code to be ececuted
}
Values received by the function, when the function is invoked, are called arguments.
The parameters and the arguments must be in the same order:
var x = myFunction(argument1, argument2);
Inside the function, the arguments can be used as local variables.

RESTRepresentational State Transfer

CORS – Cross Origin Resource Sharing 

Sum() function in LINQ



Option A:

var result = new {
NetTotal = items.Sum(t => t.net),
GrandTotal = items.Sum(t => t.total)
};
Option B:
var netGrand = items.Sum(t => t.net);
var totalGrand = items.Sum(t => t.total);
Option C:
from p in m.Items
group p by p.Id into g
select new
{
SumTotal = g.Sum(x => x.Total),
SumDone = g.Sum(x => x.Done)
}

Retrieve SCOPE_IDENTITY() value in MS Access

OleDbConnection Connection = new OleDbConnection(loan.pConnectionString);
            OleDbCommand cmdCommand = new OleDbCommand(); cmdCommand.Connection = Connection;
            OleDbTransaction transaction = null;
            try
            {
                Connection.Open();
                transaction = Connection.BeginTransaction();
                cmdCommand.Transaction = transaction;

                String sqlInsert = “INSERT INTO Loan(DebtorId,DebtorName,LoanAmount,OpeningBalance,Installment,PaidAmount,EntryDate, Remarks) VALUES(@DebtorId,@DebtorName,@LoanAmount,@OpeningBalance,@Installment,@PaidAmount, @EntryDate, @Remarks)”;

                cmdCommand.CommandText = sqlInsert;
                cmdCommand.Parameters.AddWithValue(“@DebtorId”, loan.pDebtorId);
                cmdCommand.Parameters.AddWithValue(“@DebtorName”,loan.pDebtorName);
                cmdCommand.Parameters.AddWithValue(“@LoanAmount”,loan.pLoanAmount);
                cmdCommand.Parameters.AddWithValue(“@OpeningBalance”,loan.pLoanAmount);
                cmdCommand.Parameters.AddWithValue(“@Installment”,loan.pInstallment);
                cmdCommand.Parameters.AddWithValue(“@PaidAmount”,0);
                cmdCommand.Parameters.AddWithValue(“@EntryDate”, loan.pEntryDate);
                cmdCommand.Parameters.AddWithValue(“@Remarks”,loan.pRemarks);
                cmdCommand.ExecuteNonQuery();

                cmdCommand.CommandText = “SELECT @@IDENTITY”;
                loan.pLoanId = Convert.ToInt32(cmdCommand.ExecuteScalar());

                transaction.Commit();
                Connection.Close();
   
                loan.MarkOld();
                return loan;
            }
            catch (ExceptionWithoutControl WithoutControlException) { if (transaction != null) { transaction.Rollback(); } throw WithoutControlException; }
            catch (ExceptionWithControl WithControlException) { if (transaction != null) { transaction.Rollback(); } throw WithControlException; }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw ex;
            }

Parameterized SQL Query in MS Access

OleDbConnection Connection = new OleDbConnection(loan.pConnectionString);
            OleDbCommand cmdCommand = new OleDbCommand(); cmdCommand.Connection = Connection;
            OleDbTransaction transaction = null;
            try
            {
                Connection.Open();
                transaction = Connection.BeginTransaction();
                cmdCommand.Transaction = transaction;

                String sqlInsert = “INSERT INTO Loan(DebtorId,DebtorName,LoanAmount,OpeningBalance,Installment,PaidAmount,EntryDate, Remarks) VALUES(@DebtorId,@DebtorName,@LoanAmount,@OpeningBalance,@Installment,@PaidAmount, @EntryDate, @Remarks)”;

                cmdCommand.CommandText = sqlInsert;
                cmdCommand.Parameters.AddWithValue(“@DebtorId”, loan.pDebtorId);
                cmdCommand.Parameters.AddWithValue(“@DebtorName”,loan.pDebtorName);
                cmdCommand.Parameters.AddWithValue(“@LoanAmount”,loan.pLoanAmount);
                cmdCommand.Parameters.AddWithValue(“@OpeningBalance”,loan.pLoanAmount);
                cmdCommand.Parameters.AddWithValue(“@Installment”,loan.pInstallment);
                cmdCommand.Parameters.AddWithValue(“@PaidAmount”,0);
                cmdCommand.Parameters.AddWithValue(“@EntryDate”, loan.pEntryDate);
                cmdCommand.Parameters.AddWithValue(“@Remarks”,loan.pRemarks);
                cmdCommand.ExecuteNonQuery();

                cmdCommand.CommandText = “SELECT @@IDENTITY”;
                loan.pLoanId = Convert.ToInt32(cmdCommand.ExecuteScalar());

                transaction.Commit();
                Connection.Close();
   
                loan.MarkOld();
                return loan;
            }
            catch (ExceptionWithoutControl WithoutControlException) { if (transaction != null) { transaction.Rollback(); } throw WithoutControlException; }
            catch (ExceptionWithControl WithControlException) { if (transaction != null) { transaction.Rollback(); } throw WithControlException; }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw ex;
            }