Oct 21 2009

Short way of converting file lines to Array by regex. Also pay attention to usage of HashSet

Category: C#Latif @ 23:57

 

Hi again,

 

You can see the code below that simply how to convert txt file lines to array by means of regex. Also give attention to usage of HashSet.

 

Before running the code, create a txt file named myTxtFile.txt.

 

Code with comment:

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Text.RegularExpressions;

 

namespace File2Array

{

    class Program

    {

        //Convert file lines to Array

        public static string[] FileLines2Array(string filename)

        {

            string[] returnArray;

            using (TextReader textReader = new StreamReader(new FileStream(filename, FileMode.Open)))

            {

                Regex regexForSplit = new Regex(Environment.NewLine);

                returnArray = regexForSplit.Split(textReader.ReadToEnd());

            }

            return returnArray;

        }

 

        //Convert file lines to HashSet

        public static HashSet<string> FileLines2HashSet(string filename)

        {

            HashSet<string> array2HashSet = new HashSet<string>();

            foreach (var item in FileLines2Array(filename))

            {

                array2HashSet.Add(item);

            }

            return array2HashSet;

        }

 

 

        static void Main(string[] args)

        {

            Console.WriteLine("Convert file lines to array");

            string[] testArray = FileLines2Array("myTxtFile.txt");

            foreach (var item in testArray)

            {

                Console.WriteLine(item);

            }

 

            Console.WriteLine("Delete duplicate lines automatically if any. Also see usage of HashSet");

            HashSet<string> testHashSet = FileLines2HashSet("myTxtFile.txt");

            foreach (var item in testHashSet)

            {

                Console.WriteLine(item);

            }

 

            Console.ReadKey(true);

        }

    }

}

 

 

See you soon...

Tags: , , ,

Jun 27 2009

[C#] Use a key string as an array index - Dictionary, SortedDictionary, Hashtable classes

Category: C#Latif @ 12:39

 

Hi,

   I will try to explain the differences between Dictionary, SortedDictionary, Hashtable and

at the same time,we will answer how to use a key string as an array index?

 

Namespace:  System.Collections.Generic

Dictionary(TKey, TValue) Class

SortedDictionary (TKey, TValue) Class

 

Namespace:  System.Collections

Hashtable

 

Code with explanation:

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Collections;

 

namespace genericTypes2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            //System.Collections.Generic.Dictionary

 

            Dictionary<string, string> dictionaryNew = new Dictionary<string, string>();

            //Use Add() to assign value

            dictionaryNew.Add("company", "TRIODOR");

            dictionaryNew.Add("grup", "MD INFO");

            dictionaryNew.Add("latif", "ozturk");

            dictionaryNew.Add("recep", "guzel");

            dictionaryNew.Add("talha", "turan");

            dictionaryNew.Add("pinar", "alasli");

            dictionaryNew.Add("elif", "onder");

            //You can assign value like that as well

            dictionaryNew["volkan"] = "cakar";

            dictionaryNew["bugra"] = "postaci";

            dictionaryNew["Erkan"] = "BALABAN";

 

            listBox7.Items.Add("DICTIONARY <>");

            listBox7.Items.Add(" ");

 

            //to list dictionaryNew in the ListBox - use KeyValuePair

            foreach (KeyValuePair<string, string> obj in dictionaryNew)

            {

                listBox7.Items.Add(obj.Key + " " + obj.Value);

                //other way of getting value

                //listBox7.Items.Add(dictionaryNew["" + obj.Key + ""]);

            }

 

            //System.Collections.Generic.SortedDictionary

 

            SortedDictionary<string, string> dictionaryNewSorted = new SortedDictionary<string, string>();

            //Use Add() to assign value

            dictionaryNewSorted.Add("company", "TRIODOR");

            dictionaryNewSorted.Add("grup", "MD INFO");

            dictionaryNewSorted.Add("latif", "ozturk");

            dictionaryNewSorted.Add("recep", "guzel");

            dictionaryNewSorted.Add("talha", "turan");

            dictionaryNewSorted.Add("pinar", "alasli");

            dictionaryNewSorted.Add("elif", "onder");

            //You can assign value like that as well

            dictionaryNewSorted["volkan"] = "cakar";

            dictionaryNewSorted["bugra"] = "postaci";

            dictionaryNewSorted["Erkan"] = "BALABAN";

 

            listBox8.Items.Add("SORTED DICTIONARY<>");

            listBox8.Items.Add(" ");

 

 

            //to list dictionaryNewSorted in the ListBox - use KeyValuePair

            foreach (KeyValuePair<string, string> obj in dictionaryNewSorted)

            {

                listBox8.Items.Add(obj.Key + " " + obj.Value);

            }

 

 

            //System.Collections.Hashtable

 

            Hashtable hashTableNew = new Hashtable();

            //Use Add() to assign value

            hashTableNew.Add("company", "TRIODOR");

            hashTableNew.Add("grup", "MD INFO");

            hashTableNew.Add("latif", "ozturk");

            hashTableNew.Add("recep", "guzel");

            hashTableNew.Add("talha", "turan");

            hashTableNew.Add("pinar", "alasli");

            hashTableNew.Add("elif", "onder");

            //You can assign value like that as well

            hashTableNew["volkan"] = "cakar";

            hashTableNew["bugra"] = "postaci";

            hashTableNew["Erkan"] = "BALABAN";

 

            listBox9.Items.Add("HASH TABLE<>");

            listBox9.Items.Add(" ");

           

            //to list hashTableNew in the ListBox - use DictionaryEntry

            foreach (DictionaryEntry obj in hashTableNew)

            {

                listBox9.Items.Add(obj.Key + " " + obj.Value);

            }

 

        }

    }

}

 

 

 

It is easy to see the differences between Dictionary, SortedDictionary, Hashtable in the picture above.

 

Dictionary -> uses FIFO

SortedDictionary -> sorting by array index

Hashtable -> sorting by hash algorithm

 

And also don’t forget that Dictionary is faster than others.

 

See you...

 

Tags: , , , ,

Jun 12 2009

LINQ: Max

Category: ASP.NET | LINQLatif @ 16:47

LINQ da SQL deki max islemini nasil mi yapariz?

Asagidaki sekilde,

MDDBDataContext dc = new MDDBDataContext();

var lastNumber = (from r in dc.RevenueSharings
                                  select (int?)r.StatementNumber).Max();

 

SQL server profiler tarafinda asagidaki sekilde goreceksiniz...

SELECT MAX([t0].[StatementNumber]) AS [value]
FROM [dbo].[RevenueSharing] AS [t0]

Tags: , , ,