Ejemplo n.º 1
0
        public List <User> CreateOutputList(List <User> list, sortOptions opt = sortOptions.none)
        {
            IEnumerable <Enum> flags = opt.GetSorts();
            bool        byName       = false;
            bool        byGender     = false;
            List <User> output       = new List <User>();

            foreach (Enum en in flags)
            {
                if (en.HasFlag(sortOptions.byName))
                {
                    byName = true;
                }
                if (en.HasFlag(sortOptions.byGender))
                {
                    byGender = true;
                }
            }

            if (byName && byGender)
            {
                var listQuery =
                    (from obj in list
                     select obj).OrderBy(c => c.username).ThenBy(c => c.gender);
                foreach (var v in listQuery)
                {
                    output.Add(v);
                }
            }
            else if (byName)
            {
                var listQuery =
                    (from obj in list
                     select obj).OrderBy(c => c.username);
                foreach (var v in listQuery)
                {
                    output.Add(v);
                }
            }
            else if (byGender)
            {
                var listQuery =
                    (from obj in list
                     select obj).OrderBy(c => c.gender);
                foreach (var v in listQuery)
                {
                    output.Add(v);
                }
            }
            else
            {
                Console.WriteLine("No sorting options chosen");
                var listQuery =
                    (from obj in list
                     select obj).OrderBy(c => c.gender);
                foreach (var v in listQuery)
                {
                    output.Add(v);
                }
            }

            return(output);
        }
Ejemplo n.º 2
0
        public static IEnumerable <Enum> GetSorts(this sortOptions sorts)
        {
            Enum e = sorts;

            return(Enum.GetValues(e.GetType()).Cast <Enum>().Where(e.HasFlag)); //lambada + linq + enums (COMBO)
        }