コード例 #1
0
        public IActionResult Search(string isbn)
        {
            PriceComparer comparer = new PriceComparer();
            List <Book>   books    = comparer.Compare(isbn);

            return(View("Results", books));
        }
コード例 #2
0
        public ActionResult Facade()
        {
            PriceComparer c    = new PriceComparer();
            IList <Book>  list = c.Compare(1);

            return(View());
        }
コード例 #3
0
        public override async Task <IEnumerable <Item> > GetFilteredItemsAsync(CancellationToken token = default)
        {
            var priceComparer = new PriceComparer();
            var type          = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Type).SelectedKey ?? "";
            var minPrice      = PriceConverter(Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MinPrice).SelectedKey) ?? "0 pc";
            var maxPrice      = PriceConverter(Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MaxPrice).SelectedKey) ?? "1 000 000 po";

            try
            {
                await StoreViewModel.SemaphoreLibrary.WaitAsync();

                using (var context = await StoreViewModel.GetLibraryContextAsync())
                {
                    return(context.Equipments.AsEnumerable().Where(equipment =>
                                                                   equipment.Type.ToLower().Contains(type.ToLower()) &&
                                                                   priceComparer.Compare(equipment.Price, minPrice) >= 0 &&
                                                                   priceComparer.Compare(equipment.Price, maxPrice) <= 0 &&
                                                                   (
                                                                       (Helpers.RemoveDiacritics(equipment.Name).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower())) ||
                                                                       (Helpers.RemoveDiacritics(equipment.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
                                                                   )
                                                                   ).OrderBy(eq => eq.Name)
                           .ToList());
                }
            }
            catch
            {
                return(new List <Item>());
            }
            finally
            {
                StoreViewModel.SemaphoreLibrary.Release();
            }
        }
コード例 #4
0
        public void AddMethod_BookWithComparer_BinaryTree()
        {
            var book1 = new Book()
            {
                Author           = "Рихтер Джеффри",
                ISBN             = "978-5-496-00-433-6",
                Title            = "CLR via C#",
                PageCount        = 896,
                Price            = 100,
                PublicationYear  = 2014,
                PublishingOffice = "Питер"
            };

            var book2 = new Book()
            {
                Author           = "Тепляков Сергей",
                ISBN             = "978-5-496-01649-0",
                Title            = "Паттерны проектирования",
                PageCount        = 320,
                Price            = 23,
                PublicationYear  = 2016,
                PublishingOffice = "Питер"
            };

            var book3 = new Book()
            {
                Author           = "Албахари",
                ISBN             = "978-5-496-00-433-6",
                Title            = "C# 3.0 справочник",
                PageCount        = 944,
                Price            = 70,
                PublicationYear  = 2012,
                PublishingOffice = "BHV"
            };


            var comparer = new PriceComparer();

            BinarySearchTree <Book> tree   = new BinarySearchTree <Book>(new Book[] { book1, book2, book3 }, comparer);
            List <Book>             actual = new List <Book>();

            foreach (var item in tree)
            {
                actual.Add(item);
            }

            List <Book> expected = new List <Book>()
            {
                book2, book3, book1
            };

            Assert.AreEqual(expected, actual);
        }
コード例 #5
0
 public override async Task <IEnumerable <Item> > FilterItems(IEnumerable <Item> items, CancellationToken token = default)
 {
     return(await Task.Run(() =>
     {
         var priceComparer = new PriceComparer();
         var type = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Type).SelectedKey ?? "";
         var minPrice = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MinPrice).SelectedKey ?? "0 pc";
         var maxPrice = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MaxPrice).SelectedKey ?? "1 000 000 po";
         //token.ThrowIfCancellationRequested();
         return items.Where(item =>
         {
             var equipment = item as Equipment;
             return equipment.Type.ToLower().Contains(type.ToLower()) &&
             priceComparer.Compare(equipment.Price, minPrice) >= 0 &&
             priceComparer.Compare(equipment.Price, maxPrice) <= 0 &&
             (
                 (Helpers.RemoveDiacritics(equipment.Name).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower())) ||
                 (Helpers.RemoveDiacritics(equipment.AltNameText ?? string.Empty).ToLower().Contains(Helpers.RemoveDiacritics(SearchText ?? string.Empty).ToLower()))
             );
         }).OrderBy(eq => eq.Name)
         .AsEnumerable();
     }, token));
 }
コード例 #6
0
        public ActionResult Index(string sortBy, string searchText, int priceMinRange=0, int priceMaxRange=25000)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);
           // Application.ThreadException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);
             #region Unhandled Exception Test
            //string testUnhandle = null;
            //testUnhandle.ToString();

            #endregion

            

            #region List to Array
            //List<Product> baseList = db.Products.ToList();
            //// int arraySize= baseList.Count;
            //Product[] productArray = baseList.ToArray();
            //Array productDestinationArray = Array.CreateInstance(typeof(Product), baseList.Count);
            //Array.Copy(productArray, productArray.GetLowerBound(0) + 5
            //    , productDestinationArray, productDestinationArray.GetLowerBound(0), productDestinationArray.GetLowerBound(0) + 25);

            //Array.ConstrainedCopy(productArray, productArray.GetLowerBound(0) + 5
            //  , productDestinationArray, productDestinationArray.GetLowerBound(0), productDestinationArray.GetLowerBound(0) + 5);
             
         

            #endregion

            #region List to XML
            List<Product> xmlList = db.Products.ToList();
            var xmlFromList = new XElement("Products"
                , xmlList.Select(x => new XElement("Product"
                , new  XAttribute("Id", x.ID)
                , new XAttribute("Name", x.Name)
                , new XAttribute("Price", x.Price))));
               
            #endregion
            IQueryable iqueryable;
            oLearner.Equals(oLearner);
            var hashCode= oLearner.GetHashCode();
            var typeCurrentInstance= oLearner.GetType();
            var objStr= oLearner.ToString();

            //FormCollection fc = new FormCollection();
            //fc.Add("oLearner",oLearner.ToString());
           // ArrayList aList = new ArrayList();



             
            Logger.logger.Info("Inside  Store Index Info");
            Logger.logger.Fatal("Inside  Store Index Fatal");
            Logger.logger.Error("Inside  Store Index Error");
            Logger.logger.Debug("Inside  Store Index Debug");
            Logger.logger.Warn("Inside  Store Index Warn");
           
            //string sortBy2= null;
            //if (int id == 1)
            //    sortBy = "Name"; 


            #region Locating Object for an element- IComparable IndexOf has to implement Equals n GetHashCode
            Product prod = new Product();

            prod = db.Products.Find(20);
            int locateName = db.Products.ToList().IndexOf(prod);
            List<Product> productsLambda = db.Products.ToList();
            #endregion





            if (sortBy != null && !"".Equals(sortBy))
            {

                

                PriceComparer pComparer = new PriceComparer();
                NameComparer nComparer = new NameComparer();
                //List<Product> IndexofList = db.Products.ToList();
              



                Product p1; Product p2;
                //Comparator pcomparator = new Comparator(StoresController.compare(p1,p2));

                //test(new Comparator(Compare));
                //test(new Comparator(Compare1));
                //SortedList.Sort(pComparer);
                
                //db.Products.ToList().IndexOf(prod);
                if (sortBy == "ID")
                {
                    List<Product> SortedList = db.Products.ToList();
                    SortedList.Sort();
                    return View(SortedList);
                }
                if (sortBy == "Price")
                {
                    List<Product> SortedList = db.Products.ToList();
                    #region Interface with one method 
                    //SortedList.Sort(pComparer);
                    #endregion
                    #region Anonymous Methods
                    //SortedList.Sort(delegate(Product x, Product y)
                    //                            {
                                                    
                    //                                    if (x.Price > y.Price)
                    //                                         return 1;
                    //                                    else if (x.Price < y.Price)
                    //                                        return -1;
                    //                                    else
                    //                                        return 0;

                    //                            });
                    #endregion
                    #region Named Delegate
                    //SortedList.Sort(Compare);
                    #endregion
                    #region Lambda Expressions
                    SortedList.Sort((p3,p4) => p3.Price.CompareTo(p4.Price));
                    SortedList.OrderByDescending(p => p.Price).ToList();
                    #endregion
                    return View(SortedList);
                }
                else if (sortBy == "Name") 
                {
                    List<Product> SortedList = db.Products.ToList();
                    SortedList.Sort(nComparer);
                    SortedList.Reverse();
                    return View(SortedList);
                }
                else
                {
                    return View(db.Products.ToList());
                }
            }
            else
                {
                    var products = db.Products.ToList();
                    if (searchText != null && !"".Equals(searchText))
                    {
                        products = products
                       .Where(p => p.Name.Contains(searchText) && (p.Price > priceMinRange && p.Price <= priceMaxRange)).ToList();
                        List<Product> prodList = products.ToList();
                        

                        //var productPrice = db.Products.
                        //  Where(p => (p.Price > 0 && p.Price <= 10)).ToList();
                    }
                    else
                    {
                        products = products
                      .Where(p => (p.Price > priceMinRange && p.Price <= priceMaxRange)).ToList();
                        List<Product> prodList = products.ToList();
                        

                    }

                        return View("Index", products);

                }

        }
コード例 #7
0
        public ActionResult SearchIndex(string sortBy, string searchOption ,string searchText,FormCollection formcol)
         {
             
             //string sortBy2= null;
             //if (int id == 1)
             //    sortBy = "Name";


             if (sortBy != null && !"".Equals(sortBy))
             {

                 Product prod = new Product();


                 prod = db.Products.Find(20);


                 PriceComparer pComparer = new PriceComparer();
                 NameComparer nComparer = new NameComparer();
                 List<Product> IndexofList = db.Products.ToList();
                 int locateName = db.Products.ToList().IndexOf(prod);
                 db.Products.ToList().IndexOf(prod);
                 if (sortBy == "ID")
                 {
                     List<Product> SortedList = db.Products.ToList();
                     SortedList.Sort();
                     return View("Index",SortedList);
                 }
                 if (sortBy == "Price")
                 {
                     List<Product> SortedList = db.Products.ToList();
                     SortedList.Sort(pComparer);
                     return View("Index",SortedList);
                 }
                 else if (sortBy == "Name")
                 {
                     List<Product> SortedList = db.Products.ToList();
                     SortedList.Sort(nComparer);
                     return View("Index",SortedList);
                 }
                 else
                 {
                     return View("Index",db.Products.ToList());
                 }
             }
             else
                 if (searchOption != null && searchText != null)
                 {
                     var products = db.Products
                    .Where(p => p.Name.Contains(searchText)).ToList();
                     List<Product> prodList = products.ToList();

                     var productPrice = db.Products.
                       Where(p => (p.Price > 0 && p.Price <= 10)).ToList();

                     return View("Index", productPrice);

                   
                 }
                 else
                 {
                     return View("Index",db.Products.ToList());
                 }

         }
コード例 #8
0
    protected void rptMembershipTypes_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        msMembershipType mt = (msMembershipType)e.Item.DataItem;

        if (Page.IsPostBack)
        {
            return;                             // only do this if there's a postback - otherwise, preserve ViewState
        }
        switch (e.Item.ItemType)
        {
        case ListItemType.Header:
            break;

        case ListItemType.Footer:
            break;

        case ListItemType.AlternatingItem:
            goto case ListItemType.Item;

        case ListItemType.Item:
            var             lRadioButtonMarkup     = (Literal)e.Item.FindControl("lRadioButtonMarkup");
            var             lblMembershipType      = (Label)e.Item.FindControl("lblMembershipType");
            Label           lblPrice               = (Label)e.Item.FindControl("lblPrice");
            Literal         lMembershipDescription = (Literal)e.Item.FindControl("lMembershipDescription");
            RadioButtonList rblSubCategory         = (RadioButtonList)e.Item.FindControl("rblSubCategory");
            HtmlTableRow    trSubCategory          = (HtmlTableRow)e.Item.FindControl("trSubCategory");
            HiddenField     hfMembershipType       = (HiddenField)e.Item.FindControl("hfMembershipType");
            lblMembershipType.Text = mt.Name;

            lMembershipDescription.Text = mt.Description;
            hfMembershipType.Value      = mt.ID; // important, when we need to figure out the selected membership

            /* We have to use a literal for our radio button due to an ASP.NET bug with radiobuttons
             * in repeater controls
             * http://www.asp.net/learn/data-access/tutorial-51-cs.aspx-->*/

            bool isSelected = mt.ID == selectedMembershipTypeID;        // select if necessary


            string radioButtonID = "MembershipType" + e.Item.ItemIndex;



            lRadioButtonMarkup.Text = string.Format(
                @"<input type=radio name=MembershipType " +
                @"id={0} value='{1}' {2} onclick='showAppropriateSubCategory();' />", radioButtonID,
                mt.ID,
                isSelected ? "checked" : "");


            if (allProducts == null)
            {
                return;
            }



            var relatedMembershipProducts = allProducts.FindAll(x => x.MembershipType == mt.ID);
            var relatedProducts           =
                allDescribedProducts.Where(x => relatedMembershipProducts.Exists(y => y.ProductID == x.ProductID)).ToList();

            switch (relatedProducts.Count)
            {
            case 0:
                return;         // should never happen, we filtered before this

            case 1:
                var singleProduct = relatedProducts[0];
                lblPrice.Text = singleProduct.DisplayPriceAs ?? singleProduct.Price.ToString("C");

                // let's still add it, b/c we need to reference this on the save
                rblSubCategory.Items.Add(new ListItem(
                                             string.Format("{0} - <span style='color: green'>{1}</span>", singleProduct.ProductName,
                                                           singleProduct.DisplayPriceAs ?? singleProduct.Price.ToString("C")), singleProduct.ProductID));

                trSubCategory.Visible = false;
                break;

            default:
                List <string> prices = (from p in allProducts
                                        join dp in allDescribedProducts on p.ProductID equals dp.ProductID
                                        where p.MembershipType == mt.ID
                                        select
                                        string.IsNullOrWhiteSpace(dp.DisplayPriceAs)
                                                       ? dp.Price.ToString("C")
                                                       : dp.DisplayPriceAs
                                        ).ToList();

                //MS-2090
                //If there is at least one string that starts with a price (like "$10", "15.99" or "$30/month")
                //then use our custom price sorting algorithm
                //Otherwise just sort alphabetically
                IComparer <string> sorter = StringComparer.CurrentCultureIgnoreCase;
                if (prices.Exists(PriceComparer.StartsWithPrice))
                {
                    sorter = new PriceComparer();
                }

                prices.Sort(sorter);

                if (prices.Count > 0)
                {
                    lblPrice.Text = string.Format("{0}-{1}", prices.First(), prices.Last());
                }

                trSubCategory.Visible = true;

                foreach (var p in relatedProducts)
                {
                    ListItem listItem = new ListItem(
                        string.Format("{0} - <span style='color: green'>{1}</span>", p.ProductName, p.DisplayPriceAs ?? p.Price.ToString("C")), p.ProductID);
                    if (p.ProductID == selectedMembershipFeeID)
                    {
                        listItem.Selected = true;
                    }

                    rblSubCategory.Items.Add(listItem);
                }

                rblSubCategory.SelectedIndex = 0;           // make sure the first item is selected

                if (!isSelected)
                {
                    trSubCategory.Attributes["style"] = "display: none;";           // hide it by default
                }
                // we also need to add javascript such that when this subcategory only appears
                // when the parent radio button is selected
                // so, since this is visible, we'll track it
                RadioButtonSubCategoryPairs.Add(new NameValueStringPair(radioButtonID, trSubCategory.ClientID));
                break;
            }



            break;
        }
    }