Breaking News

Monday, September 12, 2011

Phases in life of a “software developer”–Types of developers! Who am i?

Do you call yourself developer? If so, where exactly do you stand in the following table? Following table peeks into the “professional” life of a software developer


Wannabe Developer

Developer, as a profession

Senior developer

Principal developer


Chief architect


Level 0

Level 1

Level 2

Level 3

Level 4

Level 5


Copy pasting code, trying to merge; doesn’t know/care about changes

Very resistant to change; spaghetti code.

Ok to change, but provides no suggestions; tries to follow coding standards.

Ok to change, provide suggestions, brainstorm;, finds alternate solutions;

Accept changes, provide alternate solutions, face mortar board

Brings change

Thought process?

Not available (0:

Doesn’t think

Thinks about how;

Following industry standards and practices.

Provide feedbacks, comments, corrections

Build industry standards

Sticks around


Functional/method level implementation; coding

Available libraries, classes,

Reusable components

Thinking process is more in terms of business strategy; provides solutions; face mortar board, provide alternate solutions, answer any missing questions

Mortar board; business, technology

Inclines toward Coder; coding



how; componentized approach


enterprise approach


business market

Goal To go home Learning Advancement, participation,

Think around technology

Thinks around scalability, availability, modularity, loose coupling high cohesion, security, etc.

Thinks around business strategy


High light the first row of the table above, to see exactly where you stand.

Btw, no offense to anyone, these are just thoughts out of small experience that I have.

Enjoy! (0:

Read more ...

Saturday, July 30, 2011

Are you sure you want to move to gulf? Think again!

Yep, if you are moving to gulf(Dubai) from Pakistan (Karachi), it(the process) sounds like you are placing an order for a double cheese burger with diet coke!


 What is the process?

S. No.


Duration (Days)

Fee (PKR)



Get passport





Get attested degrees



HEC + UAE Consulate (PKR1200 + ~USD45)


Send to employer for visa





Receive passport/visa



~Keep 3/4 weeks of time; better resign only when you receive your visa





USD 25? @PKR86 (PKR 3000 for urgent); besides NIC


Obtain ticket with OK-TO-BOARD



Get the ticket agent that is close to your place; they usually charge 6% of the ticket charges


Get protector stamp



Very cumbersome process if you DIY





If you must, must you should


Prepare to fly



Checkout the checklist that, if you may need






STEP 1: How to prepare/renew Pakistani passport?



  1. Day 1: Get bank Challan from the National Bank that is inside Passport office, Saddar (PKR4100 Fee + PKR25 Service Charges = 4125 (as of today) – FastTrack ~5 days)
    • Requires:
      1. Old passport
      2. NIC
      3. NIC of one of your parent

Note that, if you are going for a brand new passport, they will ask you for the educational transcripts; so, inquire before you leave your place for passport office

  1. Day 2: Submit application (if you have time, and patience, you can continue this on the Day1 – but trust me, you wouldn’t want to afford that if you are with you family)
  2. Day 7: Collect passport (~next week sometime)
    • Requires:
      1. NIC


Passport office, Saddar, Karachi



STEP 2: How to get the educational documents (degree/transcripts) attested?



  1. Day 1: Attestation from HEC (Use OCS Courier Service, PKR1200/-) ~5 days. Any OCS close to your place.
    • Requires:
      1. HEC requires Matriculation/Intermediate marksheets, and certificates to be attached as well, besides your University degrees
  2. Day 6: Attestation from Foreign Ministry (Submit PKR25/-) ~2 Day process. This is right beside the FTC (Finance and Trade Center), Sharah-e-Faisal road.
    • Requires:
      1. Degree attested by HEC
  3. Day 8: Attestation from UAE Embassy (Use Bokhari Travels, USD40/- per document) ~1 day if you are willing to pay USD60/- for urgent service, otherwise USD40/- ~4 working days.
    • Requires:
      1. Degrees attested by HEC + Foreign Ministry


  • For HEC, use nearest OCS
  • Foreign Ministry, right beside FTC, Shahrah-e-Faisal
  • For UAE Consulate, go to Bokhari Travels that is on main Punjab Chowrangi



STEP 3: How to get NICOP (National Identity Card for Overseas Pakistanis)?

Please don’t ask why, it is necessary, because I don’t have the answer to that; I wonder about the reason of having multiple identifications, NIC, NICOP, Passport, and what not – for a single reason!



Somewhat similar to when filing for an NIC

  1. Issuance of Token
    • Requires:
      1. Birth Certificate —OR—
      2. Old NIC —OR—
      3. Matriculation Certificate —OR—
      4. CNICs of immediate relatives —OR—
      5. Passport (mandatory in case of new)
  2. Photo capturing
  3. Thumb and Signatures
  4. Data Acquisition by DEO
  5. Form Printing
  6. Attestation & Form Submission


Any NADRA office close to your place



STEP 4: How to get OK-TO-BOARD stamped on my visa copy?


  1. Get ticket
  2. Send your ticket/itinerary details to your employer, and ask them to apply for the OK-TO-BOARD message — OR—
    • Requires:
      1. Ticket/itinerary details
  3. Give you visa/entry permit copy to your ticket agent, and tell her to provide you with an OK-TO-BOARD stamp.
    • Requires:
      1. Visa/entry permit copy

Note: Get ticket from Emirates. OK-TO-BOARD more streamlined; suggested by a friend. It would look something like following(see hand written OK-TO-BOARD in the highlighted red):




STEP 5: How can I get protectorate stamp on my passport?

Who and why in the world would ever want it? This process/explanation might soothe your veins.



  1. Provide documents on the protectorate counter; s/he will give you a bank challan copy to sign it
    • · Requires:
      1. Passport with visa stamped
      2. NICOP or its receipt
      3. Offer letter
      4. Ticket with OK to board —OR—
      5. Attested offer letter by Ministry of foreign affairs in DUBAI

Note: You will need OK-TO-BOARD in any case, but you can’t get it stamped prior to departure.

  1. Submit challan in National Bank, UBL Bank, and Habib Bank
  2. Get photocopy of those challan on single page – not sure why its important, but it was asked with everyone
  3. Attach the challan recepts with other documents, and arrange if you can, otherwise give it to the photocopy guy; he will do that for you.
  4. Hand it over to the counter
  5. If you are lucky, they will tell you collect after 2 hours; meanwhile you roam around the Shahrah-e-Faisal sidewalks, you can enjoy a newly open Biryani shop there. ~PKR60/serving;0)

Catch99: There is a tiny Photostat shop inside the protectorate office; give all of the documents to him, and he will do the job for you, just for PKR100/- no hassle.

I did it on my own, with the help of the people around, and I did all of it. Except for the arrangements of the documents in specific order. I did not know that – and that shop guy was not willing to disclose that to me; under pressure with small amount of time left in that “sarkari (read: government)” office, I decided to let him arrange that for a cost of PKR90/- Money wasn’t the issue, it was just that I wanted to do it myself, because documents were original and I wanted to avoid any last minutes shocks – or – apologies – so to say, that he has lost the documents/etc. Anyway, throw in the money, relax, and enjoy!


  • NICOP and protector can be done through the same office
  • Takes ~1 week to receive the NICOP for urgent service
  • You can apply for Protectorate based on the receipt of the NICOP



Protectorate Office Karachi

  • If you live somewhere near North Nazimabad, there is one more office, which I have not verified, which is near Nagan Chorangi behind Salim Center, PTCL Office; a service lane is going inside, after ~200 meters, you will find the office on the left. A friend pointed out.


Read more ...

Wednesday, July 6, 2011

How to think a unified design for multi device architecture?

Couple of weeks ago, I responded to question of similar sort, and then I thought I must elaborate on this using actual code snips, that may help anyone looking for similar solution.

Problem statement:
Build an internet based application that should be able to:
  • Process the incoming requests
  • Get records available in the data store
  • Perform actions on that data
Most importantly, a non functional requirement is that, the request may come from multiple devices, for instance PC, Handheld device, iPhone, Windows Phone, Android, etc. This means, solution should have one core functional module (Unified Functionality); that would serve multiple device ends.

Simple? It is.

There could be several ways of achieving that; lets discuss couple ways, and then elaborate the implementation.

SOLUTION 1: Use WCF based RESTful service with App_Browser feature

Why? Because it is easy, tidy, and quick.

What is required to achieve that?

Mark the web service methods with WebGet attribute, and set the ResponseFormat parameter to WebMessageFormat.Json.

For instance:
[WebGet(UriTemplate = "/DoSomething?intSomeType={intSomeType}", ResponseFormat = WebMessageFormat.Json)]
Response DoSomething (Int32 intSomeType); 

When the service is accessed RESTfully, it will return the data using the DataContractJsonSerializer. So you should JSONize(JSON Serialize) your POCO’s by marking it with the [DataContract] attribute and mark each serializable member as [DataMember].
Note that, this assumes that requests will only come from browser based user agent.

Also, the type hinting in WCF can add a lot of unnecessary overhead in the JSON response for large collections. Therefore, if you believe that you might want multiple types of end-points (SOAP, etc.) at some point in the time, then WCF is the way to go.

Easy? See! Let’s go through a demo app code right out of my pilot project.

Step 1: Write an interface that defines the basic operations that a mobile service will provide; and then provide an implementation of the interface:

[ServiceContract(Namespace = "")]
public interface IMobileService
[WebGet(UriTemplate = "/ListBusinesses?intBusinessType={intBusinessType}", ResponseFormat = WebMessageFormat.Json)]
Response ListBusinesses(Int32 intBusinessType);

Step 2: Create WCF web service that implements IMobileService interface

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MobileService : IMobileService
        private NameValueCollection _params = null;

        public MobileService()
            _params = null;
            if (HttpContext.Current.Request.HttpMethod == "POST")
                _params = HttpContext.Current.Request.Params;

        public Response ListBusinesses(Int32 intBusinessType)
            return new Response(AcknowledgmentStateSuccess, ConvertToString(Business.List(intBusinessType)));

Step 3: Create a 

DataContract for a generic response:
    public class Response
        private AcknowledgmentState _ack;//custom types
        private List _errors;
        private Object _result;

        private void Initialize(AcknowledgmentState ack, List errors, object res)
            _ack = ack;
            _errors = (errors == null) ? new List() : errors;
            _result = (res == null) ? "" : res;

        public Response()
            Initialize(AcknowledgmentStateCustom, null, null);

        public Response(object res)
            Initialize(AcknowledgmentStateSuccess, null, res);

        public Response(AcknowledgmentState ack, object res)
            Initialize(ack, null, res);

        public Response(AcknowledgmentState ack, Error error, object res)
            List errors = new List(1);
            Initialize(ack, errors, res);

        public Response(AcknowledgmentState ack, List errors, object res)
            Initialize(ack, errors, res);

        [DataMember(EmitDefaultValue = true, IsRequired = true)]
        public AcknowledgmentState Ack { get { return _ack; } set { _ack = value; } }

        [DataMember(EmitDefaultValue = false, IsRequired = false)]
        public List Errors { get { return _errors; } set { _errors = value; } }

        [DataMember(EmitDefaultValue = false, IsRequired = true)]
        public Object Result { get { return _result; } set { _result = value; } }
Step 4: Create a business class that takes in the type of business, and respond with a list.

Step 5: Configure and then use the App_Browser, MOBILE.BROWSER class file
private static List List(int? businessType, int count)
            List businessList = new List();

            if (count > 100)
                count = 100;

            EnumBusinessType enumBizType = EnumBusinessType.UNKNOWN;
                if(businessType != null)
                    if(businessType > 0)
                        enumBizType = (EnumBusinessType)Enum.ToObject(typeof(EnumBusinessType), businessType);
            catch { }

            using (NorthwindDataContext dc = new NorthwindDataContext())
                var records = dc.Businesses.Select(b=> b);
                if(enumBizType != EnumBusinessType.UNKNOWN)
                    records = records.Where(b=> b.TypeID == businessType);

                if (!string.IsNullOrEmpty(prefix))
                    records = records.Where(b => b.Name.StartsWith(prefix.Trim()));

                records = records
                            .OrderBy(b => b.Name)

                foreach (var rec in records)
                    businessList.Add(new MyBusinessContract(rec.ID, rec.Name));
            return businessList;

Note that App_Browser is a feature that lets you *tweak* the way user interface is rendered based upon different devices. Its an xml based file, that uses xml to format the user interface presentation.

From MSDN:
Browser definition files contain definitions for individual browsers. At run time, ASP.NET uses the information in the request header to determine what type of browser has made the request. Then ASP.NET uses .browser files to determine the capabilities of the browser. ASP.NET control adapters can use this information to adapt the behavior of an ASP.NET Web server control depending on the type of device. For example, a server control might generate different HTML for a graphical browser such as Internet Explorer than it would for a mobile device.
I will add this .SLN on codeplex soon, ping me if I don’t. (0:

SOLUTION 2: WCF RESTful service with customized client applications

This means, separate client apps for separate devices, for instance Objective-C based app for iPhone, Silverlight based app for Windows Mobile. In this case, let’s get inside the core, and come up with a set of core functionality.

Lets decide, what the "Core" is? Or what the Core should be - what will be the functionality that Core will perform. Ideally, when we say Core, it essentially means, a layer that performs basic or atomic operations.

Over that Core you can wrap the device wrapper (IPhone, PC, etc) that builds a query to be sent to core, for instance, coming in from different device channels.
We can use WCF Data Service to constitute a data layer. And your core can perform business operations/etc, and communicate with the Data Service.

For instance:
1. Our core will performs core operations - for instance talking to Datalayer ; the Core will be a "library" that will perform basic operations:
  • Authentication
  • Authorization
  • Select records
  • Perform Action 1
  • Perform Action 2
2. A DeviceWrapper(DeviceAndriod, DeviceIPhone, DeviceWeb, etc), that wraps different devices.
The first thing that comes to mind, in this case, is a mixture of Factory and Builder pattern. Think about factory of Devices, and Builder pattern for types of views to be rendered on different devices.

3. TextRenderer, based upon the device settings, renders the text. Therefore, following will be the steps to implement:
  • Step 1: Write a core that will provide authenticate/authorization; for instance, call the Core.Authenticate(), and .Authorize();
  • Step 2: Based upon the user agent call ICore.RenderView(agentType, url), this may return you the view/html to be rendered.
User may perform an action, your request may call Core.PerformAction(); which in turn may return the link to the page to be redirected, for instance.

Up till now, we were talking about Server side, which primarily is concerned with:
  • How the requests from different user agents will be processed by the server.
  • How the text will be rendered with respect to different devices.
Client ends could be:
  1. A web browser, in this case you don't need any client app to be developed –OR–
  2. A, let’s say, Silverlight app for Windows Phone –OR–
  3. An android app for Android phone –OR–
  4. So on so forth…
Decide what this "client app" will do. For instance:
  • User shall login
  • User shall be able to perform certain operations
Note that, these client apps will call the server side smartly (using web services, rather than web pages) in order to perform operations.
Read more ...

Wednesday, June 29, 2011

Top 6 ways to parse .CSV? High Performance!

Sometime back a question was asked to develop a well performant parser– there was no restriction defined in the question whatsoever as to what technology, logic, flow/etc should be applied. Just the input block, and expected output result format, and maximum time that the parser may take.
This may answer questions:
  • How to query a .CSV and save the result in another CSV?
  • Custom .CSV Parser?
  • How to ETL .CSV into a .CSV
  • High performance .CSV parser
I found the performance requirement interesting, and so is the reason of this post; besides, another reason is to discuss only “some” of the solutions of the problem; therefore, it should be stated clearly in a question as to what exactly is required, just to save the the test’ee and tester from any after shocks. Though can have several solutions, but lets get into the details and see what do we have here. Following are important aspects of the problem.

Business requirement:

Calculate the usage of different country and dial codes for a particular customer, and write result in a separate file.

Functional requirement:
  • Read from CSV data source
  • Query for the data (select, group by, sum, count, etc)
  • Write into a separate .CSV file
Non functional requirement: (Performance)

Data source contains the ~1.2 million records, and the module is required to complete the whole procedure in less than 5 seconds.

Given that:

We have a CustomerData.CSV file already exists, to which we will query.

CustomerData.CSV has the following schema:

Columns Description
Field 1 This field contains the customer id (sorted in ascending order)
Field 2 Country code
Field 3 Dial Code
Field 4 Start time
Field 5 Call duration

Result.CSV has the following schema:

Columns Description
Field 1 Country code
Field 2 Dial Code
Field 3 Total duration (in minutes and seconds)

SOLUTION 1: (Use Jet OLE DB Text Driver)

Easiest, quickest, fastest, and very well performant!

Step 1: Define the following schema.ini file in some folder that you like:


If you would like, then look into the Schema.ini File (Text File Driver). Following content goes in schema.ini:
   1: [CustomerData.csv]
   2: Format=CSVDelimited
   3: CharacterSet=ANSI
   4: ColNameHeader=False
   5: Col1=customerId Text Width 20
   6: Col2=countryCode Short Width 3
   7: Col3=dialCode Short Width 3
   8: Col4=startTime DateTime Width 15
   9: Col5=callDuration Text Width 5
  10: [result.csv]
  11: ColNameHeader=False
  12: CharacterSet=1252
  13: Format=CSVDelimited
  14: Col1=countryCode Short
  15: Col2=dialCode Short
  16: Col3=Expr1002 Float

STEP 2: Stub in the backend code in some .cs file
   1: private static void Query(string CustomerID)
   2: {
   3:     //Pseudo/logic:
   4:     //Use jet ole db text driver to select * insert into new table; 
   5:     //to read-from a .csv, and write-into a .csv
   7:     string customerId = CustomerID;
   8:     string writeTo = @"result.csv";
   9:     string readFrom = @"CustomerData.csv";
  11:     //1: SELECT * INTO NEW_TABLE 
  12:     //2:  FROM SOURCE_TABLE 
  14:     //dont just read, write as well.
  15:     string query = @"  SELECT 
  16:                     countryCode, dialCode, sum(callDuration) INTO " + writeTo + @"
  17:                 FROM 
  18:                     [" + readFrom + @"] 
  19:                 WHERE 
  20:                     customerId='" + customerId + @"' 
  21:                 GROUP BY countryCode, dialCode";
  23:     Stopwatch timer = new Stopwatch();
  25:     try
  26:     {
  27:         Console.WriteLine("Looking for customer: {0}, to export into {1}.", customerId, writeTo);
  29:         string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\MyDocs\Software Test\;Extended Properties='text;HDR=No;FMT=CSVDelimited'";
  30:         using (OleDbConnection conn = new OleDbConnection(connectionString))
  31:         {
  32:             OleDbCommand cmd = new OleDbCommand(query, conn);
  33:             conn.Open();
  34:             timer.Start();
  35:             int nRecordsAffected = cmd.ExecuteNonQuery();
  36:             timer.Stop();
  37:             conn.Close();
  38:         }
  39:     }
  40:     catch (Exception ex) { Console.Write(ex.ToString()); }
  41:     Console.WriteLine("Time taken to read/write (ms):[{0}] ({1} secs)", timer.ElapsedMilliseconds, TimeSpan.FromMilliseconds(timer.ElapsedMilliseconds).Seconds);
  42: }



SOLUTION 2: (Write a custom class, create indexes, and apply bisection search)

For instance, following code performs following operations to achieve the same:
  1. Perform indexing on the selected column 
  2. Select specific records from the .CSV file 
  3. Perform aggregate function, call SUM() – Use LINQ
   1: using (CsvParser parser = new CsvParser(@"E:\CustomerData.csv"))
   2: {
   3:     parser.PerformIndexing((int)CsvParser.CsvColumns.Col1_CustomerID);//One time only.
   5:     timer.Start();
   6:     parser.Select("AMANTEL").Sum((int)CsvParser.CsvColumns.CallDuration);
   7:     timer.Stop();
   9:     foreach (var o in parser.Result)
  10:     {
  11:         Console.WriteLine(string.Format("CountryCode:{0}, DialCode:{1}, TotalDurationOfCall:{2}",
  12:                    o.CountryCode, o.DialCode, o.TotalDurationOfCall));
  13:     }
  15: }
  16: Console.WriteLine("Time taken to read/write (ms):[{0}] ({1} secs)", timer.ElapsedMilliseconds, TimeSpan.FromMilliseconds(timer.ElapsedMilliseconds).Seconds);



 Backend code:
   1: class CsvParser : IDisposable
   2: {
   4:     public dynamic Result { get; set; }
   5:     private string _fileName;
   6:     private char _separator = ',';
   7:     private Dictionary<string, Bounds> _lstIndex = new Dictionary<string, Bounds>();
   8:     private List<string> _Rows = new List<string>();
   9:     public enum CsvColumns { Col1_CustomerID = 0, Col2_CountryCode = 1, Col3_DialCode = 2, Col4_StartTime = 3, CallDuration = 5 }
  11:     //Simple bound structure to hold start and end index in the file.
  12:     class Bounds
  13:     {
  14:         public int Start { get; set; }
  15:         public int End { get; set; }
  17:         public Bounds(int start, int stop) { Start = start; End = stop; }
  18:     }
  21:     //Default constructor
  22:     public CsvParser(string file, char seperator = ',')
  23:     {
  24:         if (string.IsNullOrEmpty(file)) throw new Exception("Invalid file");
  26:         this._fileName = file; this._separator = seperator;
  27:     }
  29:     /// <summary>
  30:     /// Should be called once, before using the object;
  31:     /// </summary>
  32:     /// <param name="nColumn">Column to be indexed</param>
  33:     /// <returns>Chained object</returns>
  34:     public CsvParser PerformIndexing(int nColumn)
  35:     {
  36:         using (StreamReader reader = new StreamReader(_fileName))
  37:         {
  38:             string previousVal = string.Empty; string currentVal = string.Empty;
  39:             int nStart = 0;
  40:             int nEnd = 0;
  41:             int nRowCounter = 0;
  43:             do
  44:             {
  45:                 currentVal = reader.ReadLine().Split(_separator)[nColumn];
  47:                 if (previousVal != currentVal)
  48:                 {
  49:                     if (!string.IsNullOrEmpty(previousVal))
  50:                     {
  51:                         nEnd = nRowCounter;
  52:                         _lstIndex.Add(previousVal, new Bounds(nStart, nEnd)); //Add previous value
  53:                         nStart = nEnd + 1;//next line
  54:                     }
  56:                     previousVal = currentVal;
  57:                 }
  60:                 nRowCounter++;//next line
  61:             } while (!reader.EndOfStream);
  62:         }
  64:         System.Diagnostics.Trace.WriteLine(string.Format("Done. Total indexed {0}.", _lstIndex.Count));
  65:         return this;
  66:     }
  68:     /// <summary>
  69:     /// Select rows where given customer id
  70:     /// </summary>
  71:     /// <param name="CustomerID">Customer id predicate</param>
  72:     /// <returns></returns>
  73:     internal CsvParser Select(string CustomerID)
  74:     {
  75:         using (StreamReader reader = new StreamReader(_fileName))
  76:         {
  77:             //1. Get location from index; also get the next index id so that we know where to stop.
  78:             //2. Jump to that position
  79:             //3. Start fetching
  81:             Bounds bounds = _lstIndex[CustomerID];
  83:             int nRowCounter = 0;
  84:             while (!reader.EndOfStream || nRowCounter == bounds.End)
  85:             {
  86:                 if (nRowCounter >= bounds.Start)
  87:                     _Rows.Add(reader.ReadLine());
  89:                 nRowCounter++;
  91:                 if (nRowCounter > bounds.End) break;
  92:             }
  93:         }
  95:         return this;
  96:     }
  98:     /// <summary>
  99:     /// Binary search
 100:     /// </summary>
 101:     /// <param name="data"></param>
 102:     /// <param name="key"></param>
 103:     /// <param name="left"></param>
 104:     /// <param name="right"></param>
 105:     /// <returns></returns>
 106:     [Obsolete("Unused", true)]
 107:     internal static int Search(string[] data, string key, int left, int right)
 108:     {
 109:         if (left <= right)
 110:         {
 111:             int middle = (left + right) / 2;
 112:             if (key == data[middle])
 113:                 return middle;
 114:             else if (!key.Equals(data[middle]))
 115:                 return Search(data, key, left, middle - 1);
 116:             else
 117:                 return Search(data, key, middle + 1, right);
 118:         }
 119:         return -1;
 120:     }
 122:     /// <summary>
 123:     /// Provide SUM aggregate function
 124:     /// </summary>
 125:     /// <param name="nColumnID">by column</param>
 126:     /// <returns>Chained object</returns>
 127:     internal CsvParser Sum(int nColumnID)
 128:     {
 129:         var result = from theRow in _Rows
 130:                         let rowItems = theRow.Split(_separator)
 132:                         group theRow by new
 133:                         {
 134:                             countryCode = rowItems[(int)CsvColumns.Col2_CountryCode],
 135:                             dialCode = rowItems[(int)CsvColumns.Col3_DialCode]
 136:                         } into g
 138:                         select new
 139:                         {
 140:                             CountryCode = g.Key.countryCode,
 141:                             DialCode = g.Key.dialCode,
 142:                             TotalDurationOfCall = g.Sum(p => p[(int)CsvColumns.CallDuration]),
 143:                             selectedRows = g
 144:                         };
 146:         Result = result.ToList();
 148:         return this;
 149:     }
 151:     #region IDisposable Members
 152:     public void Dispose() { }
 153:     #endregion
 156: }

Btw, far more interesting code would have been the following implementation:
parser .Select(Col1, Col2, Col3)

Let me know if you can help me with that (0: Note that, I have not applied bisection search, yet; but the method is there.

SOLUTION 3: (Use TextFieldParser class)

Checkout this solution, but its a VB turned into C# solution. Let me know if you enjoy?
   1: using (var parser =
   2:     new TextFieldParser(@"c:\CustomerData.CSV")
   3:         {
   4:             TextFieldType = FieldType.Delimited,
   5:             Delimiters = new[] { "," }
   6:         })
   7: {
   8:     while (!parser.EndOfData)
   9:     {
  10:         string[] fields;
  11:         fields = parser.ReadFields();
  12:         //Do something with it!
  13:     }
  14: }


Here is how.

SOLUTION 5: (Use XmlCSVReader, convert CSV to XML and use XPath to query the data)

How so? Here is the method.

SOLUTION 6: (Load .CSV in a database and use SELECT query to get result)

See Importing CSV Data and saving it in database; I hope you get the idea; ping me if you did not. Another, just as interesting A Fast CSV Reader is also there; this is interesting because it has benchmarks.

SOLUTION 7 (Bonus): (Use Text Driver with DSN)

Just to retrieve data, quick and easy.
   1: OdbcConnection conn = new OdbcConnection("DSN=CustomerData.csv");
   2: conn.Open();
   3: OdbcCommand foo = new OdbcCommand(@"SELECT * FROM [CustomerData.csv]", conn);
   4: IDataReader dr = foo.ExecuteReader();
   5: while (dr.Read())
   6: {
   7:     List<string> data = new List<string>();
   8:     int cols = dr.GetSchemaTable().Rows.Count;
   9:     for (int i = 0; i < cols; i++)
  10:     {
  11:         Console.WriteLine(string.Format("Col:{0}", dr[i].ToString()));
  12:     }
  13: }

Happy parsing!
Read more ...
Designed By Published.. Blogger Templates