Data Structure
- IEnumerable
- IQueryable
- Dictionary and IDictionary
- ICollection
- HashTable
- IList or List
ArrayList
http://www.dotnet-tricks.com/Tutorial/csharp/bDRH230314-Understanding-Collections-and-Collections-Interfaces.html
http://www.dotnet-tricks.com/Tutorial/linq/I8SY160612-IEnumerable-VS-IQueryable.html
*******************IEnumerable***************************
IEnumerable exists in System.Collections Namespace.
MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);
SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]WHERE [t0].[EmpName] LIKE @p0
*******************IQueryable ***************************
IQueryable exists in System.Linq Namespace.
ArrayList
http://www.dotnet-tricks.com/Tutorial/linq/I8SY160612-IEnumerable-VS-IQueryable.html
*******************IEnumerable***************************
IEnumerable exists in System.Collections Namespace.
MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);
*******************IQueryable ***************************
IQueryable exists in System.Linq Namespace.
MyDataContext dc = new MyDataContext ();
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);
SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0
Notice that in this query "top 10" is exist since IQueryable executes query in SQL server with all filters.
************** HashTable*************************
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);
SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0
Hashtable weeks = new Hashtable();
weeks.Add("1", "SunDay");
weeks.Add("2", "MonDay");
weeks.Add("3", "TueDay");
weeks.Add("4", "WedDay");
weeks.Add("5", "ThuDay");
weeks.Add("6", "FriDay");
weeks.Add("7", "SatDay");
//Display a single Item
MessageBox.Show(weeks["5"].ToString ());
//Search an Item
if (weeks.ContainsValue("TueDay"))
{
MessageBox.Show("Find");
}
else
{
MessageBox.Show("Not find");
}
//remove an Item
weeks.Remove("3");
//Display all key value pairs
foreach (DictionaryEntry day in weeks )
{
MessageBox.Show (day.Key + " - " + day.Value );
}
************* ArrayList *****************
int i = 0;
ArrayList ItemList = new ArrayList();
ItemList.Add("Item4");
ItemList.Add("Item5");
ItemList.Add("Item2");
ItemList.Add("Item1");
ItemList.Add("Item3");
MessageBox.Show ("Shows Added Items");
for (i = 0; i < = ItemList.Count - 1; i++)
{
MessageBox.Show(ItemList[i].ToString());
}
//insert an item
ItemList.Insert(3, "Item6");
//sort itemms in an arraylist
ItemList.Sort();
//remove an item
ItemList.Remove("Item1");
//remove item from a specified index
ItemList.RemoveAt(3);
MessageBox.Show("Shows final Items the ArrayList");
for (i = 0; i < = ItemList.Count - 1; i++)
{
MessageBox.Show(ItemList[i].ToString());
}
***************IList or List****************
http://www.dotnet-tricks.com/Tutorial/linq/VXTY160612-IEnumerable-VS-IList.html
List < string > colors = new List < string > ();
//add items in a List collection
colors.Add("Red");
colors.Add("Blue");
colors.Add("Green");
//insert an item in the list
colors.Insert(1, "violet");
//retrieve items using foreach loop
foreach (string color in colors)
{
MessageBox.Show(color);
}
//remove an item from list
colors.Remove("violet");
//retrieve items using for loop
for (int i = 0; i < colors.Count; i++)
{
MessageBox.Show(colors[i]);
}
if (colors.Contains("Blue"))
{
MessageBox.Show("Blue color exist in the list");
}
else
{
MessageBox.Show("Not exist");
}
//copy array to list
string[] strArr = new string[3];
strArr[0] = "Red";
strArr[1] = "Blue";
strArr[2] = "Green";
List < string > arrlist = new List < string > (strArr);
foreach (string str in strArr)
{
MessageBox.Show(str);
}
//call clear method
arrlist.Clear ();
MessageBox.Show(arrlist.Count.ToString ());
}
******* Dictionary and IDictionary**********
Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out. Since both Dictionary and Hashtable are internally hashtables
Dictionary < string, int > dict = new Dictionary < string, int > ();
dict.Add("one", 1);
dict.Add("two", 2);
dict.Add("three", 3);
dict.Add("four", 4);
foreach (KeyValuePair < string, int > pair in dict)
{
MessageBox.Show(pair.Key.ToString() + " - " + pair.Value.ToString());
}
***************************************
Hashtable weeks = new Hashtable();
weeks.Add("1", "SunDay");
weeks.Add("2", "MonDay");
weeks.Add("3", "TueDay");
weeks.Add("4", "WedDay");
weeks.Add("5", "ThuDay");
weeks.Add("6", "FriDay");
weeks.Add("7", "SatDay");
//Display a single Item
MessageBox.Show(weeks["5"].ToString ());
//Search an Item
if (weeks.ContainsValue("TueDay"))
{
MessageBox.Show("Find");
}
else
{
MessageBox.Show("Not find");
}
//remove an Item
weeks.Remove("3");
//Display all key value pairs
foreach (DictionaryEntry day in weeks )
{
MessageBox.Show (day.Key + " - " + day.Value );
}
************* ArrayList *****************
int i = 0;
ArrayList ItemList = new ArrayList();
ItemList.Add("Item4");
ItemList.Add("Item5");
ItemList.Add("Item2");
ItemList.Add("Item1");
ItemList.Add("Item3");
MessageBox.Show ("Shows Added Items");
for (i = 0; i < = ItemList.Count - 1; i++)
{
MessageBox.Show(ItemList[i].ToString());
}
//insert an item
ItemList.Insert(3, "Item6");
//sort itemms in an arraylist
ItemList.Sort();
//remove an item
ItemList.Remove("Item1");
//remove item from a specified index
ItemList.RemoveAt(3);
MessageBox.Show("Shows final Items the ArrayList");
for (i = 0; i < = ItemList.Count - 1; i++)
{
MessageBox.Show(ItemList[i].ToString());
}
***************IList or List****************
http://www.dotnet-tricks.com/Tutorial/linq/VXTY160612-IEnumerable-VS-IList.html
List < string > colors = new List < string > ();
//add items in a List collection
colors.Add("Red");
colors.Add("Blue");
colors.Add("Green");
//insert an item in the list
colors.Insert(1, "violet");
//retrieve items using foreach loop
foreach (string color in colors)
{
MessageBox.Show(color);
}
//remove an item from list
colors.Remove("violet");
//retrieve items using for loop
for (int i = 0; i < colors.Count; i++)
{
MessageBox.Show(colors[i]);
}
if (colors.Contains("Blue"))
{
MessageBox.Show("Blue color exist in the list");
}
else
{
MessageBox.Show("Not exist");
}
//copy array to list
string[] strArr = new string[3];
strArr[0] = "Red";
strArr[1] = "Blue";
strArr[2] = "Green";
List < string > arrlist = new List < string > (strArr);
foreach (string str in strArr)
{
MessageBox.Show(str);
}
//call clear method
arrlist.Clear ();
MessageBox.Show(arrlist.Count.ToString ());
}
******* Dictionary and IDictionary**********
Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out. Since both Dictionary and Hashtable are internally hashtables
Dictionary < string, int > dict = new Dictionary < string, int > ();
dict.Add("one", 1);
dict.Add("two", 2);
dict.Add("three", 3);
dict.Add("four", 4);
foreach (KeyValuePair < string, int > pair in dict)
{
MessageBox.Show(pair.Key.ToString() + " - " + pair.Value.ToString());
}
***************************************
Operation
Algorithm = > Operation
http://auk-port.blogspot.com/2012/08/difference-among-ienumerable-iqueryable_5620.html
http://auk-port.blogspot.com/2012/08/difference-among-ienumerable-iqueryable_5620.html
http://people.cs.aau.dk/~normark/oop-csharp/html/notes/collections_themes-list-sect.html
http://www.dotnet-tricks.com/Tutorial/linq/I8SY160612-IEnumerable-VS-IQueryable.html
No comments:
Post a Comment