Tuesday, October 16, 2012

Selecting Columns in a Data Table in C#

“How to select multiple columns in a Data Table” … This is one of the questions asked by a reader in my article published at C# Corner site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/querying-a-data-table-using-select-method-and-lambda-express/

So, I am showing here how we can do this. Here are three ways by which we could select multiple columns in a DataTable object.

(1) Using LINQ

This is a quite flexible way to do this. In this approach, we could do a lot using LINQ e.g. setting filter criteria over the rows, making joins of the table etc.     

   1:  var query = (
   2:                              from r in dt.AsEnumerable()
   3:                              select new
   4:                              {
   5:                                  SSN = r["SSN"].ToString(),
   6:                                  NAME = r["NAME"].ToString(),
   7:                              }
   8:                          ).ToList();
   9:   
  10:              foreach (var item in query)
  11:              {
  12:                  Console.WriteLine(item.SSN + "\t" + item.NAME);
  13:              }
 
(2) Using DataView class:

This is also a way to get a the view on Data table.

   1:              DataView view = new DataView(dt);
   2:              DataTable distinctValues = view.ToTable("dt", true, "SSN", "NAME");
   3:   
   4:              foreach (DataRow o in distinctValues.Select())
   5:              {
   6:                  Console.WriteLine("\t" + o["SSN"] + "\t" + o["NAME"]);
   7:              }

(3) Using DataTable.Rows collection

If we use this approach, we don’t have much scope. We could only enumerate the rows, read the rows, edit the rows like that…

   1:              for (int i = 0; i < dt.Rows.Count; i++)
   2:              {
   3:                  Console.WriteLine(dt.Rows[i]["SSN"]+ "\t"+ dt.Rows[i]["NAME"]);
   4:              }
 

Monday, October 15, 2012

C# DataTable Select Examples - using Lambda Expressions

Querying a Data Table Using Select Method and Lambda Expressions in C# 

For this article, Click the link below. A few days before, I wrote this article for C-Sharp Corner site:


http://www.c-sharpcorner.com/UploadFile/0f68f2/querying-a-data-table-using-select-method-and-lambda-express/

Friday, October 12, 2012

Setting Timeouts in WCF Service-Client side for a long running process


With a WCF service and its client, often I forget which timeout should be set on which side..?
There are four types of timeout parameters which we set in a WCF service:
• closeTimeout (Default value: 00:01:00)
• openTimeout (Default value: 00:01:00)
• receiveTimeout (Default value: 00:10:00)
• sendTimeout (Default value: 00:01:00)


Here is a thumb rule (at least for my need to run a long process :)

To execute a long running process, we need to set “receiveTimeout” on Service side same as “sendTimeout” on the client side.
For example we could set “00:30:00” (30 minutes)


For more details, here are definitions from MDSN:

Client-side Timeouts

On the client side:
1. SendTimeout – used to initialize the OperationTimeout, which governs the whole process of sending a message, including receiving a reply message for a request/reply service operation. This timeout also applies when sending reply messages from a callback contract method.
2. OpenTimeout – used when opening channels when no explicit timeout value is specified
3. CloseTimeout – used when closing channels when no explicit timeout value is specified
4. ReceiveTimeout – is not used


Service-side Timeouts

On the service side:
1. SendTimeout, OpentTimeout, CloseTimeout are the same as on the client
2. ReceiveTimeout – used by the Service Framework Layer to initialize the session-idle timeout which controls how long a session can be idle before timing out.