private void TinhTongSoChuCaiXuatHien(string chuoi) { chuoi = String.Join("", chuoi.Where(c => !char.IsWhiteSpace(c))); chuoi = chuoi.Trim().ToUpper(); List <string> mangKiTuXuatHien = new List <string>(); /*chuoi * .Where(Char.IsLetterOrDigit) * .GroupBy(c => c) * .Aggregate((seed, next) => * { * mangKiTuXuatHien.Add(next.Key + ": " + next.Count()); * return seed; * }); * richTextBoxSoChuCaiCau4.Text = String.Join("\n", mangKiTuXuatHien); * txtTongSoChuCaiXuatHienCau4.Text = mangKiTuXuatHien.Count().ToString();*/ var FreQ = from x in chuoi group x by x into y select y; foreach (var ArrEle in FreQ) { mangKiTuXuatHien.Add(ArrEle.Key + ": " + ArrEle.Count()); } richTextBoxSoChuCaiCau4.Text = String.Join("\n", mangKiTuXuatHien); txtTongSoChuCaiXuatHienCau4.Text = mangKiTuXuatHien.Count().ToString(); }
static void Main(string[] args) { List <string> words = new List <string>() { "the", "bike", "this", "it", "tenth", "mathematics" }; var outputList = words.Where(x => x.Contains("th")).ToList(); foreach (var word in words) { Console.WriteLine(word); } List <string> classGrades = new List <string>() { "80,100,92,89,65", "93,81,78,84,69", "73,88,83,99,64", "98,100,66,74,55" }; var gradeAverage = classGrades.ToList(); List <int> studentGrade = new List <int>(); double studentGradeAverage = 0; foreach (var result in gradeAverage) { studentGrade = result.Split(',').Select(m => int.Parse(m)).ToList(); //Console.WriteLine(result); var studentAverage = studentGrade.Where(x => x != studentGrade.Min()).Average(); studentGradeAverage += studentAverage; } Console.WriteLine("Class Average is: " + studentGradeAverage / 4); Console.ReadLine(); string userInput = Console.ReadLine(); var counts = from x in userInput group x by x into y select y; foreach (var ArrEle in counts) { Console.WriteLine("Character " + ArrEle.Key + ": " + ArrEle.Count() + " times"); } Console.ReadLine(); }
private void TinhTongSoCapTrung(string chuoi) { chuoi = String.Join("", chuoi.Where(c => !char.IsWhiteSpace(c))); chuoi = chuoi.Trim().ToUpper(); List <string> mangCapTrung = new List <string>(); List <string> mangTam = new List <string>(); /*if (chuoi.StartsWith("0x")) * { * //return * MessageBox.Show(chuoi.Skip(2) * .Select((x, i) => new { index = i, value = x }) * .GroupBy(pair => pair.index / 2) * .Select(grp => string.Join("", grp.Select(x => x.value))) * .Select(x => Convert.ToByte(x, 16)) * .ToArray().ToString()); * }*/ int j = 1; for (int i = 0; i < chuoi.Length; i++) { if (j == chuoi.Length) { break; } else { string cap = ""; cap = cap + chuoi[i] + chuoi[j]; mangTam.Add(cap); j++; } } var FreQ = from x in mangTam group x by x into y select y; foreach (var ArrEle in FreQ) { if (ArrEle.Count() > 1) { mangCapTrung.Add(ArrEle.Key + ": " + ArrEle.Count()); } } txtTongSoCapTrungCau4.Text = mangCapTrung.Count().ToString(); richTextBoxCapTrungCau4.Text = String.Join("\n", mangCapTrung); }
//////////////////////////////////////////////////////////////////////////////////ENCRYPT /// public static string Encrypt(string text, int n) //{ // if (n <= 0) // { // return text; // } // else // { // for (int j = n; j >= 1; j--) // { // string s = ""; //string s2 = ""; // for (int i = 0; i<text.Length; i++) // { // if (i % 2 == 1) // { // s += text[i]; // } // else // s2 += text[i]; // } // text = String.Concat(s, s2); // } // return text; // } //} // public static string Encrypt(string text, int n) // { // if (text == "" || text == null || n <= 0) { return text; } // Sanity Check // string odd = ""; // string even = ""; // for (int i = 0; i < n; i++) // { // for (int j = 0; j < text.Length; j++) // { // switch (j % 2) // { // case 0: // even += text[j]; // break; // case 1: // odd += text[j]; // break; // default: // Console.WriteLine("An error occurred."); // break; // } // //Console.WriteLine("Index: " + (j < 10 ? "0" + j.ToString() : j.ToString()) + " Char: " + text[j] + " Text: " + "Odd: " + odd + " Even: " + even); // }//end encryption iteration // text = odd + even; // odd = even = ""; // }//end n times // return text; // } // public static string Decrypt(string encryptedText, int n) // { // if (encryptedText == "" || encryptedText == null || n <= 0) { return encryptedText; } // Sanity Check // int len = encryptedText.Length; // string evenHalf = ""; // string oddHalf = ""; // for (int i = 0; i < n; i++) // { // evenHalf = encryptedText.Substring(0, len / 2); // oddHalf = encryptedText.Substring(len / 2, (1 + len) / 2); // //Console.WriteLine("\n Decrypt Iteration " + i + "\n Even String: " + evenHalf + " Odd String: " + oddHalf); // encryptedText = ""; // for (int j = 0; j < len; j++) // { // switch (j % 2) // { // case 0: // encryptedText += oddHalf[0]; // oddHalf = oddHalf.Substring(1); // break; // case 1: // encryptedText += evenHalf[0]; // evenHalf = evenHalf.Substring(1); // break; // default: // Console.WriteLine("An error occurred."); // break; // } // //Console.WriteLine("Index: " + (j < 10 ? "0" + j.ToString() : j.ToString()) + " Char: " + encryptedText[j] + " Text: " + encryptedText + " Odd: " + oddHalf + " Even: " + evenHalf); // } // end decrypt iteration // } // end n times // return encryptedText; // } //} //////////////////////////////////////////////Program Count digts >1 public static int DuplicateCount(string str) { string s = ""; var FreQ = from x in str.ToLower() group x by x into y select y; foreach (var ArrEle in FreQ) { if (ArrEle.Count() > 1) { s += ArrEle.Key; } } return(s.Length); }
static void Main(string[] args) { string str; Console.WriteLine("Specify a string:"); str = Console.ReadLine(); var FreQ = from x in str group x by x into y select y; Console.WriteLine("The frequency of characters:"); foreach (var ArrEle in FreQ) { Console.WriteLine("Character " + ArrEle.Key + " occurs " + ArrEle.Count() + " times"); } Console.ReadKey(); }
static void Main(string[] args) { string str; Console.Write("\nLINQ : Display the characters and frequency of character from giving string : "); Console.Write("\n----------------------------------------------------------------------------\n"); Console.Write("Input the string : "); str = Console.ReadLine(); Console.Write("\n"); var FreQ = from x in str group x by x into y select y; Console.Write("The frequency of the characters are :\n"); foreach (var ArrEle in FreQ) { Console.WriteLine("Character " + ArrEle.Key + ": " + ArrEle.Count() + " times"); } }
public static void Main() { Linq l = new Linq(); l.Linq1(); l.Linq5(); DupString stringdemo = new DupString(); stringdemo.duplicateString(); int[] arr = new int[] { 1, 2, 3, 4, 5, 2, 3 }; var arr1 = from s in arr group s by s into g select g; Console.ReadKey(); string str; Console.Write("\nLINQ : Display the characters and frequency of character from giving string : "); Console.Write("\n----------------------------------------------------------------------------\n"); Console.Write("Input the string : "); str = Console.ReadLine(); Console.Write("\n"); var FreQ = from x in str group x by x into y select y; Console.Write("The frequency of the characters are :\n"); foreach (var ArrEle in FreQ) { Console.WriteLine("Character " + ArrEle.Key + ": " + ArrEle.Count() + " times"); } Console.ReadKey(); getPersonList(); }
static void Main(string[] args) { string input = "1"; while (input != "0") { Console.WriteLine("Welcome :) Press 1 to 8 for general practices and 0 to Close"); input = Console.ReadLine(); if (input == "1") { int[] n1 = { 1, 3, -2, -4, -7, -3, -8, 12, 19, 6, 9, 10, 14 }; Console.Write("\nLINQ : Using multiple WHERE clause to find the positive numbers within the list : "); Console.Write("\n-----------------------------------------------------------------------------"); var nQuery = from VrNum in n1 where VrNum > 0 where VrNum < 12 select VrNum; Console.Write("\nThe numbers within the range of 1 to 11 are : \n"); foreach (var VrNum in nQuery) { Console.Write("{0} ", VrNum); } Console.Write("\n\n"); } else if (input == "2") { var arr1 = new[] { 3, 9, 2, 8, 6, 5 }; Console.Write("\nLINQ : Find the number and its square of an array which is more than 20 : "); Console.Write("\n------------------------------------------------------------------------\n"); var sqNo = from int Number in arr1 let SqrNo = Number * Number where SqrNo > 20 select new { Number, SqrNo }; foreach (var a in sqNo) { Console.WriteLine(a); } } else if (input == "3") { int[] arr1 = new int[] { 5, 9, 1, 2, 3, 7, 5, 6, 7, 3, 7, 6, 8, 5, 4, 9, 6, 2 }; Console.Write("\nLINQ : Display the number and frequency of number from given array : \n"); Console.Write("---------------------------------------------------------------------\n"); Console.Write("The numbers in the array are : \n"); Console.Write(" 5, 9, 1, 2, 3, 7, 5, 6, 7, 3, 7, 6, 8, 5, 4, 9, 6, 2\n"); var n = from x in arr1 group x by x into y select y; Console.WriteLine("\nThe number and the Frequency are : \n"); foreach (var arrNo in n) { Console.WriteLine("Number " + arrNo.Key + " appears " + arrNo.Count() + " times"); } Console.WriteLine("\n"); } else if (input == "4") { string str; Console.Write("\nLINQ : Display the characters and frequency of character from giving string : "); Console.Write("\n----------------------------------------------------------------------------\n"); Console.Write("Input the string : "); str = Console.ReadLine(); Console.Write("\n"); var FreQ = from x in str group x by x into y select y; Console.Write("The frequency of the characters are :\n"); foreach (var ArrEle in FreQ) { Console.WriteLine("Character " + ArrEle.Key + ": " + ArrEle.Count() + " times"); } } else if (input == "5") { string[] arr1 = { "aaa.frx", "bbb.TXT", "xyz.dbf", "abc.pdf", "aaaa.PDF", "xyz.frt", "abc.xml", "ccc.txt", "zzz.txt" }; Console.Write("\nLINQ : Count File Extensions and Group it : "); Console.Write("\n------------------------------------------\n"); Console.Write("\nThe files are : aaa.frx, bbb.TXT, xyz.dbf,abc.pdf"); Console.Write("\n aaaa.PDF,xyz.frt, abc.xml, ccc.txt, zzz.txt\n"); Console.Write("\nHere is the group of extension of the files : \n\n"); var fGrp = arr1.Select(file => Path.GetExtension(file).TrimStart('.').ToLower()) .GroupBy(z => z, (fExt, extCtr) => new { Extension = fExt, Count = extCtr.Count() }); foreach (var m in fGrp) { Console.WriteLine("{0} File(s) with {1} Extension ", m.Count, m.Extension); } } else if (input == "6") { List <string> listOfString = new List <string>(); listOfString.Add("m"); listOfString.Add("n"); listOfString.Add("o"); listOfString.Add("p"); listOfString.Add("q"); Console.Write("\nLINQ : Remove Items from List using remove function : "); Console.Write("\n----------------------------------------------------\n"); var _result1 = from y in listOfString select y; Console.Write("Here is the list of items : \n"); foreach (var tchar in _result1) { Console.WriteLine("Char: {0} ", tchar); } string newstr = listOfString.FirstOrDefault(en => en == "o"); listOfString.Remove(newstr); var _result = from z in listOfString select z; Console.Write("\nHere is the list after removing the item 'o' from the list : \n"); foreach (var rChar in _result) { Console.WriteLine("Char: {0} ", rChar); } } else if (input == "7") { char[] charset1 = { 'X', 'Y', 'Z' }; int[] numset1 = { 1, 2, 3, 4 }; Console.Write("\nLINQ : Generate a Cartesian Product of two sets : "); Console.Write("\n------------------------------------------------\n"); var cartesianProduct = from letterList in charset1 from numberList in numset1 select new { letterList, numberList }; Console.Write("The Cartesian Product are : \n"); foreach (var productItem in cartesianProduct) { Console.WriteLine(productItem); } } else if (input == "8") { List <Item_mast> itemlist = new List <Item_mast> { new Item_mast { ItemId = 1, ItemDes = "Ahmad " }, new Item_mast { ItemId = 2, ItemDes = "Ducks" }, new Item_mast { ItemId = 3, ItemDes = "Danish " }, new Item_mast { ItemId = 4, ItemDes = "Qaiser " }, new Item_mast { ItemId = 5, ItemDes = "Haroon " } }; List <Purchase> purchlist = new List <Purchase> { new Purchase { InvNo = 100, ItemId = 3, PurQty = 800 }, new Purchase { InvNo = 101, ItemId = 2, PurQty = 650 }, new Purchase { InvNo = 102, ItemId = 3, PurQty = 900 }, new Purchase { InvNo = 103, ItemId = 4, PurQty = 700 }, new Purchase { InvNo = 104, ItemId = 3, PurQty = 900 }, new Purchase { InvNo = 105, ItemId = 4, PurQty = 650 }, new Purchase { InvNo = 106, ItemId = 1, PurQty = 458 } }; Console.Write("\nLINQ : Generate a Left Join between two data sets : "); Console.Write("\n--------------------------------------------------\n"); Console.Write("Here is the Item_mast List : "); Console.Write("\n-------------------------\n"); foreach (var item in itemlist) { Console.WriteLine( "Item Id: {0}, Description: {1}", item.ItemId, item.ItemDes); } Console.Write("\nHere is the Purchase List : "); Console.Write("\n--------------------------\n"); foreach (var item in purchlist) { Console.WriteLine( "Invoice No: {0}, Item Id : {1}, Quantity : {2}", item.InvNo, item.ItemId, item.PurQty); } Console.Write("\nHere is the list after joining : \n\n"); var leftOuterJoin = from itm in itemlist join prch in purchlist on itm.ItemId equals prch.ItemId into a from b in a.DefaultIfEmpty(new Purchase()) select new { itid = itm.ItemId, itdes = itm.ItemDes, prqty = b.PurQty }; Console.WriteLine("Item ID\t\tItem Name\tPurchase Quantity"); Console.WriteLine("-------------------------------------------------------"); foreach (var data in leftOuterJoin) { Console.WriteLine(data.itid + "\t\t" + data.itdes + "\t\t" + data.prqty); } } } }