Пример #1
0
        static void Main(string[] args)
        {
            //整数の例
            var numbers = new List <int> {
                19, 17, 15, 24, 12, 25, 14, 20, 12, 28, 19, 30, 24
            };

            var strings = numbers.Distinct().ToArray();

            foreach (var str in strings)
            {
                Console.Write(str + " ");
            }

            Console.WriteLine();//改行
            numbers.Distinct().Select(n => n.ToString("0000")).ToList().ForEach(s => Console.Write(s + " "));

            //並べ替え
            Console.WriteLine();//改行
            var sortedNumbers = numbers.OrderBy(n => n);

            foreach (var nums in sortedNumbers)
            {
                Console.Write(nums + " ");
            }


            //文字列の例
            Console.WriteLine("\n\n----------------");
            var words = new List <string> {
                "Microsoft", "Apple", "Google", "Oracle", "Facebook",
            };

            var lower = words.Select(name => name.ToLower()).ToArray();


            //Bookオブジェクト
            Console.WriteLine("\n\n----------------");
            var books = Books.GetBooks();
            //タイトルリスト
            var titles = books.Select(name => name.Title);

            foreach (var title in titles)
            {
                Console.Write(title + " ");
            }


            //ページ数の多い順に並べ替え(または金額の高い順)
            var sortedBooks = books.OrderByDescending(book => book.Pages);

            foreach (var book in sortedBooks)
            {
                Console.WriteLine(book.Title + " " + book.Pages);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var numbers = new List <int> {
                9, 7, -5, 4, 2, 5, 4, 2 - 4, 8, -1, 6, 4
            };

            Console.WriteLine($"平均値:{numbers.Average()}");
            Console.WriteLine($"合計値:{numbers.Sum()}");
            Console.WriteLine($"最小値:{numbers.Where(n => n > 0).Min()}");
            Console.WriteLine($"最大値;{numbers.Max()}");

            var books = Books.GetBooks();

            var number = new List <int> {
                8, 20, 15, 48, 2
            };
            var strings = number.Select(n => n.ToString("0000")).ToArray();

            foreach (var i in strings)
            {
                Console.WriteLine(i);
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
#if true
            #region 問題6-1
            //整数の例
            var numbers =
                new int[] { 5, 10, 17, 9, 3, 21, 10, 40, 21, 3, 35, };

            Console.WriteLine("------6.1.1------");
            Console.WriteLine($"最大値:{numbers.Max()}");

            Console.WriteLine("\n------6.1.2------");
            int pos = numbers.Length - 2;
            foreach (var number in numbers.Skip(pos))
            {
                Console.Write(number + ",");
            }

            Console.WriteLine("\n------6.1.3------");
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write(numbers[i].ToString() + ",");
            }

            Console.WriteLine("\n------6.1.4------");
            var min = numbers.OrderBy(s => s).Take(3);
            foreach (var mn in min)
            {
                Console.Write(mn + ",");
            }

            Console.WriteLine("\n------6.1.5------");
            var disend = numbers.Distinct().Where(s => s > 10);
            foreach (var dis in disend)
            {
                Console.Write(dis + ",");
            }
            #endregion
#endif
#if true
            #region 問題6-2
            Console.WriteLine("\n------6.2.1------");
            var book = Books.GetBooks().Where(s => s.Title == "ワンダフル・C#ライフ");
            foreach (var books in book)
            {
                Console.Write($"価格:{books.Price}円,ページ数:{books.Pages}");
            }

            Console.WriteLine("\n------6.2.2------");
            Console.WriteLine(Books.GetBooks().Count(s => s.Title.Contains("C#")));

            Console.WriteLine("\n------6.2.3------");
            Console.WriteLine(Books.GetBooks().Where(s => s.Title.Contains("C#")).Average(s => s.Pages));

            Console.WriteLine("\n------6.2.4------");
            Console.WriteLine(Books.GetBooks().FirstOrDefault(s => s.Price >= 4000).Title);

            Console.WriteLine("\n------6.2.5------");
            Console.WriteLine(Books.GetBooks().Where(s => s.Price < 4000).Max(s => s.Pages));

            Console.WriteLine("\n------6.2.6------");
            var orderPrice = Books.GetBooks().Where(s => s.Pages >= 400).OrderByDescending(s => s.Price);
            foreach (var price in orderPrice)
            {
                Console.WriteLine($"タイトル:{price.Title},価格:{price.Price}円");
            }

            Console.WriteLine("\n------6.2.7------");
            var titles = Books.GetBooks().Where(s => s.Title.Contains("C#")).Where(s => s.Pages <= 500);
            foreach (var title in titles)
            {
                Console.WriteLine($"{title.Title}");
            }

            //全ての書籍から「C#」の文字がいくつあるかをカウントする
            int count = 0;

            foreach (var book1 in Books.GetBooks().Where(b => b.Title.Contains("C#")))
            {
                for (int i = 0; i < book1.Title.Length - 1; i++)
                {
                    if ((book1.Title[i] == 'C') && (book1.Title[i + 1] == '#'))
                    {
                        count++;
                    }
                }
            }

            Console.WriteLine($"文字列「C#]の個数は{count}です。");

            #endregion
#endif
        }
Пример #4
0
        static void Main(string[] args)
        {
            //整数の例
            var numbers = new int[] { 5, 10, 17, 9, 3, 21, 10, 40, 21, 3, 35 };

            var strings = numbers.Distinct().ToArray();

            foreach (var str in strings)
            {
                Console.Write(str + " ");
            }
            Console.WriteLine();//改行
            var sortedNumber = numbers.OrderBy(n => n);

            foreach (var nums in sortedNumber)
            {
                Console.Write(nums + " ");
            }

            Console.WriteLine();//改行
            //文字の例
            var words = new List <string> {
                "Microsoft", "Aplle", "Google", "Oracle", "Facebook",
            };
            var Lowes = words.Select(nameof => nameof.ToLower()).ToArray();
            //オブジェクトの例
            var books = Books.GetBooks();
            //タイトルのみリスト
            var titles = books.Select(name => name.Title);

            foreach (var title in titles)
            {
                Console.WriteLine(title + " ");
            }

            Console.WriteLine();//改行
            var sortedBooks = books.OrderByDescending(x => x.Pages);

            //foreach (var book in sortedBooks)
            //{
            //    Console.WriteLine( book.Title + " "+book.Pages);
            //}

            Console.WriteLine();//改行
            Console.WriteLine($"最大値は{ numbers.Where(n => n > 0).Max()}です。");

            int pos = numbers.Length - 2;

            foreach (var number in numbers.Skip(pos))
            {
                Console.WriteLine(number + " ");
            }

            var strNum = numbers.Select(n => n.ToString());

            foreach (var strnum in strNum)
            {
                Console.Write(strnum + " ");
            }

            Console.WriteLine();
            foreach (var item in numbers.OrderBy(n => n).Take(3))
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.WriteLine(numbers.Distinct().Count(n => n > 10));

            {
            }

            var bookss = new List <Book> {
                new Book {
                    Title = "C#プログラミングの新常識「c#」", Price = 3800, Pages = 378
                },
                new Book {
                    Title = "ラムダ式とLINQの極意", Price = 2500, Pages = 312
                },
                new Book {
                    Title = "ワンダフル・C#ライフ", Price = 2900, Pages = 385
                },
                new Book {
                    Title = "一人で学ぶ並列処理プログラミング", Price = 4800, Pages = 464
                },
                new Book {
                    Title = "フレーズで覚えるC#入門", Price = 5300, Pages = 604
                },
                new Book {
                    Title = "私でも分かったASP.NET MVC", Price = 3200, Pages = 453
                },
                new Book {
                    Title = "楽しいC#プログラミング教室", Price = 2540, Pages = 348
                },
            };
            //全ての書籍から「C#」の文字がいくつあるのかカウントする

            int count = 0;

            foreach (var bookkk in books.Where(b => b.Title.Contains("C#")))
            {
                for (int i = 0; i < bookkk.Title.Length - 1; i++)
                {
                    if ((bookkk.Title[i] == 'c') && (bookkk.Title[i + 1] == '#'))
                    {
                        count++;
                    }
                }
            }
            Console.WriteLine($"文字列「C#」の個数は{count}です。");

            //6-1
            var book = bookss.Where(n => n.Title == "ワンダフル・C#ライフ");

            foreach (var booksss in book)
            {
                Console.WriteLine($"{booksss.Title} {booksss.Price}円  P{booksss.Pages}");
            }

            //6-2
            Console.WriteLine(bookss.Count(x => x.Title.Contains("C#")));
            //6-3
            Console.WriteLine(bookss.Where(x => x.Title.Contains("C#")).Average(x => x.Pages));

            //6-4
            Console.WriteLine(bookss.FirstOrDefault(x => x.Price >= 4000).Title);

            //6-5
            Console.WriteLine(bookss.Where(x => x.Price < 4000).Max(x => x.Pages));

            //6-6

            foreach (var item in bookss.Where(x => x.Pages >= 400).OrderByDescending(x => x.Price))
            {
                Console.WriteLine(item.Title + " " + item.Price);
            }

            //6-7
            foreach (var item in bookss.Where(x => x.Title.Contains("C#") && x.Pages <= 500))
            {
                Console.WriteLine(item.Title);
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            //6.1
            //var numbers = new int[] { 5, 10, 17, 9, 3, 21, 10, 40, 21, 3, 35 };
            ////6.1.1
            //Console.WriteLine(numbers.Max(num => num));
            //Console.WriteLine();
            ////6.1.2
            //var results = numbers.Length - 2;
            //foreach (var item in numbers.Skip(results))
            //{
            //    Console.WriteLine(item);
            //}

            //Console.WriteLine();
            ////6.1.3
            //var ress = numbers.Select(n => n.ToString("000")).ToList();
            //foreach (var item in ress)
            //{
            //    Console.WriteLine(item);

            //}
            //Console.WriteLine();
            ////6.1.4
            //var sort=numbers.OrderBy(n=>n).Take(3);
            //foreach (var s in sort)
            //{
            //    Console.WriteLine(s);
            //}
            //Console.WriteLine();
            ////6.1.5
            //var counts = numbers.Distinct().Count(n => n > 10);
            //Console.WriteLine($"10以上の数は{counts}個です");
            //Console.WriteLine();

            ////6.2.1
            var books = Books.GetBooks();
            //var book = books.Where(n => n.Title == "ワンダフル・C#ライフ");
            //foreach (var item in book)
            //{
            //    Console.WriteLine($"価格:{item.Price}  ページ{item.Pages}");
            //}
            //Console.WriteLine();
            ////6.2.2
            //Console.WriteLine($"タイトルに「C#」がある冊数:{books.Count(x => x.Title.Contains("C#"))}");
            //Console.WriteLine();
            ////6.2.3
            //var title = books.Where(n => n.Title.Contains("C#"));
            //Console.WriteLine(title.Average(a => a.Pages));
            //Console.WriteLine();
            ////6.2.4
            //var bookIndex = books.FirstOrDefault(m => m.Price >= 4000);
            //if (bookIndex!=null)
            //{
            //    Console.WriteLine(bookIndex.Title);
            //}
            //Console.WriteLine();
            ////6.2.5
            //var bookmans = books.Where(m => m.Price < 4000).Max(n=>n.Pages);
            //Console.WriteLine(bookmans);
            //Console.WriteLine();
            ////6.2.6
            //var bookoff = books.Where(m => m.Pages >= 400).OrderByDescending(n => n.Price);
            //foreach (var item in bookoff)
            //{
            //    Console.WriteLine("{0},{1}", item.Title, item.Price);
            //}
            //Console.WriteLine();
            ////6.2.7
            //var selected = books.Where(n => n.Title.Contains("C#") && n.Pages <= 500);
            //foreach (var item in selected)
            //{
            //    Console.WriteLine(item.Title);
            //}
            //Console.WriteLine();
            //全ての書籍から「C#」の文字がいくつあるかカウントする
            int count = 0;

            foreach (var book in books.Where(b => b.Title.Contains("C#")))
            {
                for (int i = 0; i < book.Title.Length - 1; i++)
                {
                    if ((book.Title[i] == 'C') && (book.Title[i + 1] == '#'))
                    {
                        count++;
                    }
                }
            }
            Console.WriteLine($"文字列「C#」の個数は{count}です");
        }
Пример #6
0
        static void Main(string[] args)
        {
            var numbers = new int[] { 5, 10, 17, 9, 3, 21, 10, 40, 21, 3, 35 };
            var books   = Books.GetBooks();

            #region 問題6-1
            //問題6-1-1
            //Console.WriteLine("問題6-1-1");
            //Console.WriteLine(numbers.Max());
            //問題6-1-2
            //Console.WriteLine("問題6-1-2");
            //int pos = numbers.Length - 2;
            //numbers.Skip(pos).ToList().ForEach(f => Console.Write(f + " "));
            //問題6-1-3
            //Console.WriteLine();
            //Console.WriteLine("問題6-1-3");
            //numbers.Select(s => s.ToString()).ToList().ForEach(f => Console.Write(f + " "));
            //問題6-1-4
            //Console.WriteLine();
            //Console.WriteLine("問題6-1-4");
            //numbers.OrderBy(o => o).Take(3).ToList().ForEach(f => Console.Write(f + " "));
            //問題6-1-5
            //Console.WriteLine();
            //Console.WriteLine("問題6-1-5");
            //Console.WriteLine(numbers.Distinct().Count(c => c > 10));
            #endregion

            #region 問題6-2
            //問題6-2-1

            Console.WriteLine("問題6-2-1");
            var book = books.Where(f => f.Title == "ワンダフル・C#ライフ");
            //var book = books.FirstOrDefault(f => f.Title == "ワンダフル・C#ライフ");

            foreach (var item in book)
            {
                Console.WriteLine($"価格:{item.Price} ページ数:{item.Pages}");
            }

            //問題6-2-2
            Console.WriteLine("問題6-2-2");
            var selected = books.Count(c => c.Title.Contains("C#"));
            Console.WriteLine($"{selected}冊");

            //問題6-2-3
            Console.WriteLine("問題6-2-3");
            var sa = books.Where(c => c.Title.Contains("C#")).Average(a => a.Pages);
            Console.WriteLine(sa);

            //問題6-2-4
            Console.WriteLine("問題6-2-4");
            var bookp = books.Where(w => w.Price >= 4000);
            foreach (var bookps in bookp)
            {
                Console.WriteLine(bookps.Title);
            }

            //問題6-2-5
            Console.WriteLine("問題6-2-5");
            var pt = books.Where(w => w.Price < 4000).Max(m => m.Pages);
            Console.WriteLine(pt);

            //問題6-2-6
            Console.WriteLine("問題6-2-6");
            var bookpt = books.Where(w => w.Pages >= 400).OrderByDescending(o => o.Price);
            foreach (var bookpts in bookpt)
            {
                Console.WriteLine($"{bookpts.Title} {bookpts.Price}");
            }

            //問題6-2-7
            Console.WriteLine("問題6-2-7");
            var bookppt = books.Where(w => w.Title.Contains("C#") && w.Pages <= 500);
            foreach (var bookppts in bookppt)
            {
                Console.WriteLine(bookppts.Title);
            }
            #endregion

            //すべての書籍から、「C#」の文字がいくつあるかをカウントする
            //int count = 0;
            //foreach (var book in books.Where(b => b.Title.Contains("C#")))
            //{
            //    for (int i = 0; i < book.Title.Length-1; i++)
            //    {
            //        if ((book.Title[i] == 'C')&&(book.Title[i+1]=='#'))
            //        {
            //            count++;
            //        }
            //    }
            //}
            //Console.WriteLine($"文字列「C#」の個数は{count}です。");
        }
Пример #7
0
        static void Main(string[] args)
        {
            #region P.163まで
            //var numbers = Enumerable.Range(1, 20).ToArray();

            //foreach (var item in numbers)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion
            #region P.175
            //var numbers = new List<int> { 9, 7, -5, 4, 2, 5, 4, 2, -4, 8, -1, 6, 4, };
            //Console.WriteLine($"平均値:{numbers.Average()}");
            //Console.WriteLine($"合計値:{numbers.Sum()}");
            //Console.WriteLine($"最小値:{numbers.Where(n => n > 0).Min()}");
            //Console.WriteLine($"最大値:{numbers.Min()}");

            ////7の倍数があるのか
            //bool exists = numbers.Any(n => n % 7 == 0);

            //var results = numbers.Where(n => n > 0).Take(3);

            //foreach (var result in results)
            //{
            //    Console.Write(result + " ");
            //}

            //Console.WriteLine("\n-----------------------------");
            //var books = Books.GetBooks();
            //Console.WriteLine($"本の平均価格:{books.Average(x => x.Price)}");
            //Console.WriteLine($"本の合計価格:{books.Sum(x => x.Price)}");
            //Console.WriteLine($"本のページが最大:{books.Max(x => x.Pages)}");
            //Console.WriteLine($"高価な本:{books.Max(x => x.Price)}");
            //Console.WriteLine($"タイトルに「物語」がある冊数:{books.Count(x => x.Title.Contains("物語"))}");

            ////600ページを超える書籍があるか?(Anyメソッド)
            ////if (books.Any(n => n.Pages > 600))
            ////{
            ////    Console.WriteLine("存在する");
            ////}
            //Console.Write("600ページを超える書籍は");
            //Console.WriteLine(books.Any(x => x.Pages > 600) ? "ある" : "ない");


            ////すべてが200ページ以上の書籍化?(Allメソッド)
            ////if (books.All(n=>n.Pages>=200))
            ////{
            ////    Console.WriteLine("すべて200ページ以上です");
            ////}
            //Console.WriteLine();//改行
            //Console.Write("600ページを超える書籍は");
            //Console.WriteLine(books.All(x => x.Pages >= 200) ? "あります" : "ありません");

            ////400ページを超える本は何冊か?(FirstOrDefaultメソッド)
            //Console.WriteLine();//改行
            ////var book = books.FirstOrDefault(x => x.Pages > 400);
            ////int i;
            ////for (i = 0; i < books.Count; i++)
            ////{
            ////    if (books[i].Title.Contains(book.Title))
            ////    {
            ////        break;
            ////    }
            ////}
            //var count = books.FindIndex(x => x.Pages > 400);
            ////Console.WriteLine($"400ページを超える本は{i + 1}冊目です");
            //Console.WriteLine($"400ページを超える本は{count + 1}冊目です");


            ////本の値段が400円以上のものを3冊表示
            //Console.WriteLine();//改行

            //Console.WriteLine("本の値段が400円以上は");
            //var rt = books.Where(n => n.Price >= 400).Take(3);
            //foreach (var item in rt)
            //{
            //    Console.WriteLine(item.Title);
            //}
            #endregion

            //整数の例
            var numbers = new List <int>
            {
                19, 17, 15, 24, 12, 25, 14, 20, 12, 28, 19, 30, 24
            };

            //var strings = numbers.Select(n => n.ToString("0000")).ToArray();
            var strings = numbers.Distinct().ToArray();
            foreach (var str in strings)
            {
                Console.Write(str + " ");
            }
            numbers.Select(n => n.ToString("0000")).ToList().ForEach(s => Console.Write(s + " "));

            //並べ替え
            Console.WriteLine();
            var sortedNumbers = numbers.OrderBy(n => n);
            foreach (var nums in sortedNumbers)
            {
                Console.WriteLine(nums + " ");
            }

            //文字列の例
            Console.WriteLine("\n\n-----------------------");
            var words = new List <string>
            {
                "Microsoft", "Apple", "Google", "Oracle", "Facebook",
            };

            var lower = words.Select(x => x.ToLower()).ToArray();

            //オブジェクトの例
            Console.WriteLine("\n\n-----------------------");
            var books = Books.GetBooks();

            //タイトルのみのリスト
            var titles = books.Select(name => name.Title);

            foreach (var title in titles)
            {
                Console.Write(titles + " ");
            }
            Console.WriteLine("\n\n-----------------------");

            //ページの多い順に並べ替え(または金額の高い順)
            var sortedBooks = books.OrderByDescending(book => book.Pages);
            foreach (var book in sortedBooks)
            {
                Console.WriteLine(book.Title + " " + book.Pages);
            }
        }