Ejemplo n.º 1
0
        /// <summary>
        /// Add a cached association between at least 2 ids. Using less than 2 IDs will cause an exception
        /// </summary>
        /// <param name="synthbotId"></param>
        /// <param name="spotifyId"></param>
        /// <param name="discordId"></param>
        public void Add(string synthbotId = null, string spotifyId = null, string discordId = null)
        {
            // If there are less than 2 params specified, throw exception
            // We cant make an association with only 1 id
            var synthbotIdNull = string.IsNullOrWhiteSpace(synthbotId) ? 0 : 1;
            var spotifyIdNull  = string.IsNullOrWhiteSpace(spotifyId) ? 0 : 1;
            var discordIdNull  = string.IsNullOrWhiteSpace(discordId) ? 0 : 1;
            var nullParamCount = synthbotIdNull + spotifyIdNull + discordIdNull;

            if (nullParamCount < 2)
            {
                string name = string.Empty;
                name = synthbotIdNull == 1 ? nameof(synthbotId) : name;
                name = spotifyIdNull == 1 ? nameof(spotifyId) : name;
                name = discordIdNull == 1 ? nameof(discordId) : name;
                throw new ArgumentNullException(name);
            }

            var group = new UserIdGroup()
            {
                SynthbotId = synthbotId,
                DiscordId  = discordId,
                SpotifyId  = spotifyId
            };

            Add(group);
        }
Ejemplo n.º 2
0
 public void Add(UserIdGroup group)
 {
     if (!string.IsNullOrWhiteSpace(group.SynthbotId))
     {
         _synthbotIds[group.SynthbotId] = group;
     }
     if (!string.IsNullOrWhiteSpace(group.SpotifyId))
     {
         _spotifyIds[group.SpotifyId] = group;
     }
     if (!string.IsNullOrWhiteSpace(group.DiscordId))
     {
         _discordIds[group.DiscordId] = group;
     }
 }
Ejemplo n.º 3
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 users1)
            {
                Console.WriteLine(value.Name);
            }

            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: "******" - " + post.Count);
            }

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

            var userLatAndLong = from user in allUsers
                                 where (user.Address.Geo.Lat < 0) && (user.Address.Geo.Lng < 0)
                                 select user;
            foreach (var user in userLatAndLong)
            {
                Console.WriteLine(user.Name + " -> " + user.Address.Geo.Lat + " and " + user.Address.Geo.Lng);
            }

            // 5 - find the post with longest body.

            var maxLength = allPosts.Select(post => post.Body.Length).Max();
            var max       = from post in allPosts
                            where post.Body.Length == maxLength
                            select post;

            foreach (var post in  max)
            {
                Console.WriteLine(post.Id + " - " + post.Body + " - " + maxLength);
            }

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

            var innerJoin = allUsers.Join(
                allPosts,
                user => user.Id,
                post => post.UserId,
                (user, post) => new
            {
                Body = post.Body,
                Name = user.Name
            });
            foreach (var obj in innerJoin)
            {
                if (maxLength == obj.Body.Length)
                {
                    Console.WriteLine(obj.Name);
                }
            }

            // 7 - select all addresses in a new List<Address>. print the list.
            var addresses = from item in allUsers
                            select item.Address;
            List <Address> addresses1 = new List <Address>();
            foreach (var item in addresses)
            {
                addresses1.Add(item);
            }
            foreach (var item in addresses1)
            {
                Console.WriteLine(item.Street + ", " + item.Suite + ", " + item.City + ", " + item.Zipcode + ", " + item.Geo.Lat + ", " + item.Geo.Lat);
            }

            // 8 - print the user with min lat
            var minLat = (from user in allUsers
                          select user.Address.Geo.Lat).Min();
            var min = from user in allUsers
                      where user.Address.Geo.Lat == minLat
                      select user;
            foreach (var user in min)
            {
                Console.WriteLine(user.Name + " - " + minLat);
            }

            // 9 - print the user with max long
            var maxLong = (from user in allUsers
                           select user.Address.Geo.Lng).Max();
            var maxL = from user in allUsers
                       where user.Address.Geo.Lng == maxLong
                       select user;
            foreach (var user in maxL)
            {
                Console.WriteLine(user.Name + " - " + 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


            var      postUsers = allPosts.GroupBy(post => post.UserId, post => post);
            UserPost userPosts = new UserPost();

            List <UserPost> userPosts1 = new List <UserPost>();
            //neterminat


            // 11 - order users by zip code
            var ZipCode = allUsers.OrderBy(user => user.Address.Zipcode);
            foreach (var user in ZipCode)
            {
                Console.WriteLine($"{user.Address.Zipcode} - {user.Name}");
            }

            // 12 - order users by number of posts
            var OrderUsers = sum.OrderBy(user => user.Count);

            foreach (var user in OrderUsers)
            {
                Console.WriteLine($"{user.UserId} - {user.Count}");
            }

            Console.ReadLine();
        }