New paste Repaste Download
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Numerics;
namespace L25
{
    //результат = from переменная in источник_данных select переменная
    static class WordCountExtensions
    {
        public static int WordCount(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return 0;
            string data = Regex.Replace(str.Trim(), @"\s+", " ");
            return data.Split(' ').Length;
        }
    }
    class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int GroupId { get; set; }
    }
    class Group
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 5, 34, 67, 12, 94, 42 };
            IEnumerable<int> query = from i in ar
                                     where i % 2 == 0
                                     orderby i descending//ascending
                                     select i;
            foreach (int i in query)
            {
                Console.Write($"{i}\t");
            }
            Console.WriteLine();
            ar[0] = 24;
            foreach (int i in query)
            {
                Console.Write($"{i}\t");
            }
            Console.WriteLine();
            IEnumerable<IGrouping<int, int>> query2 = from i in ar
                                                      group i by i % 10;
            foreach (IGrouping<int, int> group in query2)
            {
                Console.Write($"Group: {group.Key}\nValue: ");
                foreach (int item in group)
                {
                    Console.Write($"{item}\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine($"\nOnly group with count > 1: ");
            var query3 = from i in ar
                        group i by i % 10 into res
                        where res.Count() > 1
                        select res;
            foreach (var group in query3)
            {
                Console.Write($"Group: {group.Key}\nValue: ");
                foreach (int item in group)
                {
                    Console.Write($"{item}\t");
                }
                Console.WriteLine();
            }
            string[] poem = {"All the world's a stage,",
                "And all the men and women merely players; ",
                "They have their exits and their entrances,",
                "And one man in his time plays many parts,",
                "His acts being seven ages…"};
            IEnumerable<string> query4 = from line in poem
                                         let words = line.Split(' ', ';', ',')
                                         from w in words
                                         where w.Count() > 5
                                         select w;
            foreach (string item in query4)
            {
                Console.WriteLine($"\t{item}");
            }
            List<Group> groups = new List<Group>
            {
                new Group { Id = 1, Name = "27PPS11" },
                new Group { Id = 2, Name = "27PPS12" }
            };
            List<Student> students = new List<Student> {
                new Student { FirstName = "John", LastName = "Miller", GroupId = 2 },
                new Student { FirstName = "Candice", LastName = "Leman", GroupId = 1 },
                new Student { FirstName = "Joey", LastName = "Finch", GroupId = 1 },
                new Student { FirstName = "Nicole", LastName = "Taylor", GroupId = 2 }
            };
            var query5 = from g in groups
                        join st in students
                        on g.Id equals st.GroupId
                        into res
                        from r in res
                        select r;
            Console.WriteLine("Students in groups:");
            foreach (var s in query5)
            {
                Console.WriteLine($"{s.LastName} {s.FirstName}, " +
                    $"group {groups.First(g => g.Id == s.GroupId).Name}");
            }
            var a = 5;
            /*var number;
            var salary = null;*/
            var person = new {
                FirstName = "John",
                LastName = "Doe",
                Salary = 17456.36m
            };
            Console.WriteLine(person);
            Console.WriteLine($"{person.GetType()}");
            Console.WriteLine($"{person.GetHashCode()}");
            var query6 = from g in groups
                         join st in students
                         on g.Id equals st.GroupId
                         select new
                         {
                             FirstName = st.FirstName,
                             LastName = st.LastName,
                             GroupName = g.Name
                         };
            foreach (var s in query6)
            {
                Console.WriteLine(s);
            }
        }
    }
}
Filename: None. Size: 5kb. View raw, , hex, or download this file.

This paste expires on 2024-06-14 07:28:38.989497. Pasted through web.