示例#1
0
        private static void Main(string[] args)
        {
            List <User> allUsers = ReadUsers("users.json");
            List <Post> allPosts = ReadPosts("posts.json");

            #region Demo

            // 1 - find all users having email ending with ".net".
            var users1 = from user in allUsers
                         where user.Email.EndsWith(".net")
                         select user;

            var users11 = allUsers.Where(user => user.Email.EndsWith(".net"));

            IEnumerable <string> userNames = from user in allUsers
                                             select user.Name;

            var userNames2 = allUsers.Select(user => user.Name);

            foreach (var value in userNames2)
            {
                Console.WriteLine(value);
            }

            IEnumerable <Company> allCompanies = from user in allUsers
                                                 select user.Company;

            var users2 = from user in allUsers
                         orderby user.Email
                         select user;

            var netUser = allUsers.First(user => user.Email.Contains(".net"));
            Console.WriteLine(netUser.Username);

            #endregion

            // 2 - find all posts for users having email ending with ".net".
            IEnumerable <int> usersIdsWithDotNetMails = from user in allUsers
                                                        where user.Email.EndsWith(".net")
                                                        select user.Id;

            IEnumerable <Post> posts = from post in allPosts
                                       where usersIdsWithDotNetMails.Contains(post.UserId)
                                       select post;

            foreach (var post in posts)
            {
                Console.WriteLine(post.Id + " " + "user: "******"UserId: {0}, Count: {1} ", group.userId, group.count);
            }



            // 4 - find all users that have lat and long negative.
            var UsersFromWestOrSouth = allUsers
                                       .Where(user => { if (user.Address == null || user.Address.Geo == null)
                                                        {
                                                            return(false);
                                                        }
                                                        return(user.Address.Geo.Lat < 0 || user.Address.Geo.Lng < 0); });
            foreach (var wrongGeo in UsersFromWestOrSouth)
            {
                Console.WriteLine("UserId : {0}, Lat: {1}, Lng : {2}", wrongGeo.Id, wrongGeo.Address.Geo.Lat, wrongGeo.Address.Geo.Lng);
            }


            // 5 - find the post with longest body.
            var orderedPostsByLength = (from post in allPosts
                                        orderby post.Body.Length
                                        descending
                                        select post).First();
            Console.WriteLine(orderedPostsByLength.Id + " -> " + orderedPostsByLength.Body);
            // 6 - print the name of the employee that have post with longest body.

            var postUser = (from user in allUsers where user.Id == orderedPostsByLength.UserId select user).First();
            Console.WriteLine("User => Id: {0}, Name: {1}", postUser.Id, postUser.Name);
            // 7 - select all addresses in a new List<Address>. print the list.

            List <Address> addresses = (from user in allUsers select user.Address).ToList();
            addresses.DisplayAddress();

            // 8 - print the user with min lat
            var minLatUser = (from user in allUsers orderby user.Address.Geo.Lat ascending select user).First();
            Console.WriteLine("User: {0}, Lat: {1}", minLatUser.Name, minLatUser.Address.Geo.Lat);

            // 9 - print the user with max long
            var maxLngUser = (from user in allUsers orderby user.Address.Geo.Lng descending select user).First();
            Console.WriteLine("User: {0}, Lat: {1}", maxLngUser.Name, maxLngUser.Address.Geo.Lng);

            // 10 - create a new class: public class UserPosts { public User User {get; set}; public List<Post> Posts {get; set} }
            //    - create a new list: List<UserPosts>
            //    - insert in this list each user with his posts only
            List <UserPosts> userPosts = new List <UserPosts>();
            var userPostsAll           =
                (from user in allUsers
                 join post in allPosts on user.Id equals post.UserId into postGroup
                 select new { User = user, Posts = postGroup });
            foreach (var tmp in userPostsAll)
            {
                UserPosts uPost = new UserPosts();
                uPost.User  = tmp.User;
                uPost.Posts = tmp.Posts.ToList();
                userPosts.Add(uPost);
            }

            // 11 - order users by zip code

            var zipSortedUsers = allUsers.OrderBy(user => user.Address.Zipcode);
            foreach (var zipUser in zipSortedUsers)
            {
                Console.WriteLine("UserId : {0}, NAme: {1}, Zipcode : {2}", zipUser.Id, zipUser.Name, zipUser.Address.Zipcode);
            }
            // 12 - order users by number of posts
            var userPostsSorted =
                (from user in allUsers
                 join post in allPosts on user.Id equals post.UserId into postGroup
                 select new { UserName = user.Name, Posts = postGroup.Count() }).OrderBy(data => data.Posts);
            foreach (var group in userPostsSorted)
            {
                Console.WriteLine("UserId: {0}, Count: {1} ", group.UserName, group.Posts);
            }
        }
        private static void Main(string[] args)
        {
            List <User> allUsers = ReadUsers("users.json");
            List <Post> allPosts = ReadPosts("posts.json");

            #region Demo

            // 1 - find all users having email ending with ".net".
            var users1 = from user in allUsers
                         where user.Email.EndsWith(".net")
                         select user;

            var users11 = allUsers.Where(user => user.Email.EndsWith(".net"));

            IEnumerable <string> userNames = from user in allUsers
                                             select user.Name;

            var userNames2 = allUsers.Select(user => user.Name);

            //            foreach (var value in userNames2)
            //            {
            //                Console.WriteLine(value);
            //            }

            IEnumerable <Company> allCompanies = from user in allUsers
                                                 select user.Company;

            var users2 = from user in allUsers
                         orderby user.Email
                         select user;

            var netUser = allUsers.First(user => user.Email.Contains(".net"));
            //            Console.WriteLine(netUser.Username);

            #endregion

            // 2 - find all posts for users having email ending with ".net".
            var usersIdsWithDotNetMails = from user in allUsers
                                          where user.Email.EndsWith(".net")
                                          select user.Id;

            var posts = allPosts.Where(post => usersIdsWithDotNetMails.Contains(post.UserId));

            //            foreach (var post in posts)
            //            {
            //                Console.WriteLine(post.Id + " " + "user: "******"{nrOfPost.userName} {nrOfPost.Posts}");
            //            }

            // 4 - find all users that have lat and long negative.

            var ugn = allUsers.Where(allUser => allUser.Address.Geo.Lat < 0 && allUser.Address.Geo.Lng < 0)
                      .Select(allUser => allUser.Username);


            var z = from user in allUsers
                    where user.Address.Geo.Lat < 0 && user.Address.Geo.Lng < 0
                    select user;
            //            foreach (var u in ugn)
            //            {
            //                Console.WriteLine(u);
            //            }

            // 5 - find the post with longest body.
            var postLogestBody     = (allPosts.Select(p => p.Body.Length)).Max();
            var postWithLogestBody = (allPosts.Where(p => p.Body.Length == postLogestBody));
            //            foreach (var post in postWithLogestBody)
            //            {
            //                Console.WriteLine(post.Body);
            //            }



            // 6 - print the name of the employee that have post with longest body.

            var nameEmpWithLogBody = allUsers
                                     .Join(allPosts, user => user.Id, post => post.UserId, (user, post) => new { user, post })
                                     .Where(@t => @t.post.Body.Length == postLogestBody)
                                     .Select(@t => @t.user.Username);
            //            foreach (var x in nameEmpWithLogBody)
            //            {
            //                Console.WriteLine(x);
            //            }



            // 7 - select all addresses in a new List<Address>. print the list.
            List <Address> allAddresses = (allUsers.Select(us => us.Address)).ToList();
            //                foreach (var allAddress in allAddresses)
            //                {
            //                    Console.WriteLine(allAddress.Geo.Lat);
            //                    Console.WriteLine(allAddress.Geo.Lng);
            //                    Console.WriteLine(allAddress.City);
            //                    Console.WriteLine(allAddress.Street);
            //                    Console.WriteLine(allAddress.Suite);
            //                    Console.WriteLine(allAddress.Zipcode);
            //                    Console.WriteLine();
            //            }

            // 8 - print the user with min lat
            var minLat         = (allAddresses.Select(add => add.Geo.Lat)).Min();
            var userWithMinLat = allUsers.Where(user => user.Address.Geo.Lat.Equals(minLat));

            // 9 - print the user with max long
            var maxLong          = (allAddresses.Select(add => add.Geo.Lat)).Max();
            var userWithMaxLongt = allUsers.Where(user => user.Address.Geo.Lat.Equals(maxLong));


            // 10 - create a new class: public class UserPosts { public User User {get; set}; public List<Post> Posts {get; set} }
            //    - create a new list: List<UserPosts>
            //    - insert in this list each user with his posts only

            List <UserPosts> userPost = new List <UserPosts>();
            var xxx = (from post in allPosts
                       join user in allUsers on post.UserId equals user.Id
                       group user by new
            {
                user,
                post
            }
                       into allPostsForUser
                       select new
            {
                allPostsForUser.Key.user,
                allPostsForUser.Key.post
            }).ToList();

            foreach (var t in xxx)
            {
                var up = new UserPosts();
                up.User = t.user;
                up.Posts.Add(t.post);
                userPost.Add(up);
            }

            foreach (var up in userPost)
            {
                Console.WriteLine(up.User.Username);

                foreach (var p in up.Posts)
                {
                    Console.WriteLine(p.Body);
                }

                Console.WriteLine();
                Console.WriteLine();
            }



            // 11 - order users by zip code
            var usersOrderdByZip = allUsers.OrderBy(x => x.Address.Zipcode);


            // 12 - order users by number of posts

            var nrOfPostsForEachUser = allUsers.Join(allPosts, u => u.Id, p => p.UserId, (u, p) => new { u, p })
                                       .GroupBy(@t => new { @t.u.Username, @t.p.UserId }, @t => @t.u)
                                       .Select(all => new { user = all.Key, Posts = all, }).OrderBy(x => x.Posts.Count());

            foreach (var n in nrOfPostsForEachUser)
            {
                Console.WriteLine(n.user.Username);
            }
        }