Example #1
0
        public Linqs()
        {
            var demoList = DemoData.GetList(10);

            #region FirstOrDefault + Where
            // Hint: Single element vs List
            var firstMatchedElement = demoList.FirstOrDefault(element => element.MyInt > 10);
            var allMatchedElements  = demoList.Where(element => element.MyInt > 10);

            //var onlyFirstMatchedElement = demoList.First(element => element.MyInt < 2);
            #endregion

            #region All + Any
            var allElementsBigger400 = demoList.All(element => element.MyInt > 400);
            var anyElementBigger400  = demoList.Any(element => element.MyInt > 400);
            #endregion

            #region Min + Max
            var minValue = demoList.Min(element => element.MyInt);
            var maxValue = demoList.Max(element => element.MyInt);
            #endregion

            #region Ordering
            // Hint: OrderedList-Type
            var demoListOrderedAsc  = demoList.OrderBy(element => element.MyInt);
            var demoListOrderedDesc = demoList.OrderByDescending(element => element.MyInt);
            #endregion

            #region Paging incl. Chaining
            var skipped = demoList.Skip(10);
            var taken   = demoList.Take(10);

            // Chained: skipped and taken
            var skippedAndTaken = demoList.Skip(10).Take(10);
            #endregion

            #region Conversion with select
            var convertedList = demoList.Select(element => new { CustomInt = element.MyInt });
            #endregion

            #region Others
            //demoList.Distinct() // IEqualityComparer
            #endregion

            #region Grouping
            var groupedItems = demoList.GroupBy(element => element.MyBool);

            //var groupedItmes2 = demoList.GroupBy(element => element.MyBool).Select(group => new { Key = group.Key, Amount = group.Count() });
            #endregion
        }
Example #2
0
        public BasicLoops()
        {
            #region debug
            ConsoleHelper.DrawSection();
            #endregion

            #region string-demo
            //var s = "";
            //s += demoList[i].MyString;
            #endregion

            var demoList = DemoData.GetList(3);
            for (int i = 0; i < demoList.Count; i++)
            {
                //Console.WriteLine(demoList[i].MyString);

                //demoList.Remove(demoList[i]);
            }

            #region debug
            ConsoleHelper.DrawSection();
            #endregion

            foreach (var demoListItem in demoList)
            {
                //Console.WriteLine(demoListItem.MyString);

                //demoList.Remove(demoListItem);
            }

            #region debug
            ConsoleHelper.DrawSection();
            #endregion

            var counter = 0;
            while (counter < demoList.Count)
            {
                //Console.WriteLine(demoList[counter].MyString);
                counter++;
            }

            #region debug
            ConsoleHelper.DrawSection();
            #endregion
        }