public List <ProductWithFeatures> ProductsWithAllTheFeaturesFromTheList(ICollection <Feature> features)
        {
            Func <ProductWithFeatures, bool> doesItHaveAllTheFeatures = x =>
            {
                FeatureComparer comparer = new FeatureComparer();
                bool            b        = features.All(y => x.Features.Contains(y, comparer));
                return(b);
            };

            return(products.Where(doesItHaveAllTheFeatures).ToList());
        }
Пример #2
0
        public void CheckIfProductsWithNoneOfTheFeaturesFromTheListWorksCorrectly()
        {
            LinqHomework.Feature[] ids = new LinqHomework.Feature[6];
            ids[0] = new LinqHomework.Feature(1);
            ids[1] = new LinqHomework.Feature(3);
            ids[2] = new LinqHomework.Feature(5);
            ids[3] = new LinqHomework.Feature(7);
            ids[4] = new LinqHomework.Feature(9);
            ids[5] = new LinqHomework.Feature(11);

            List <LinqHomework.ProductWithFeatures> list = new List <LinqHomework.ProductWithFeatures>();

            LinqHomework.Feature[] featuresA = new LinqHomework.Feature[7];
            featuresA[0] = new LinqHomework.Feature(2);
            featuresA[1] = new LinqHomework.Feature(5);
            featuresA[2] = new LinqHomework.Feature(3);
            featuresA[3] = new LinqHomework.Feature(9);
            featuresA[4] = new LinqHomework.Feature(4);
            featuresA[5] = new LinqHomework.Feature(8);
            featuresA[6] = new LinqHomework.Feature(7);
            LinqHomework.ProductWithFeatures productA = new LinqHomework.ProductWithFeatures();
            productA.Name     = "a";
            productA.Features = featuresA;
            list.Add(productA);

            LinqHomework.Feature[] featuresB = new LinqHomework.Feature[8];
            featuresB[0] = new LinqHomework.Feature(11);
            featuresB[1] = new LinqHomework.Feature(4);
            featuresB[2] = new LinqHomework.Feature(2);
            featuresB[3] = new LinqHomework.Feature(7);
            featuresB[4] = new LinqHomework.Feature(9);
            featuresB[5] = new LinqHomework.Feature(3);
            featuresB[6] = new LinqHomework.Feature(1);
            featuresB[7] = new LinqHomework.Feature(5);
            LinqHomework.ProductWithFeatures productB = new LinqHomework.ProductWithFeatures();
            productB.Name     = "b";
            productB.Features = featuresB;
            list.Add(productB);

            LinqHomework.Feature[] featuresC = new LinqHomework.Feature[2];
            featuresC[0] = new LinqHomework.Feature(2);
            featuresC[1] = new LinqHomework.Feature(4);
            LinqHomework.ProductWithFeatures productC = new LinqHomework.ProductWithFeatures();
            productC.Name     = "c";
            productC.Features = featuresC;
            list.Add(productC);

            LinqHomework.Feature[] featuresD = new LinqHomework.Feature[6];
            featuresD[0] = new LinqHomework.Feature(1);
            featuresD[1] = new LinqHomework.Feature(3);
            featuresD[2] = new LinqHomework.Feature(5);
            featuresD[3] = new LinqHomework.Feature(7);
            featuresD[4] = new LinqHomework.Feature(9);
            featuresD[5] = new LinqHomework.Feature(11);
            LinqHomework.ProductWithFeatures productD = new LinqHomework.ProductWithFeatures();
            productD.Name     = "d";
            productD.Features = featuresD;
            list.Add(productD);

            LinqHomework.ProductClassHomework productList = new LinqHomework.ProductClassHomework(list);

            List <LinqHomework.ProductWithFeatures> final = new List <LinqHomework.ProductWithFeatures>();

            LinqHomework.Feature[] features1 = new LinqHomework.Feature[2];
            features1[0] = new LinqHomework.Feature(2);
            features1[1] = new LinqHomework.Feature(4);
            LinqHomework.ProductWithFeatures product1 = new LinqHomework.ProductWithFeatures();
            product1.Name     = "c";
            product1.Features = features1;
            final.Add(product1);

            var x = new ProductComparer();
            var y = productList.ProductsWithNoneOfTheFeaturesFromTheList(ids).ToArray();
            var z = final.ToArray();

            bool a = false, b = false;

            if (y[0].Name == z[0].Name)
            {
                a = true;
            }

            var         featureComparer = new LinqHomework.FeatureComparer();
            List <bool> boolList = new List <bool>();

            foreach (var p in y[0].Features)
            {
                bool zahar = z[0].Features.Contains(p, featureComparer);
                boolList.Add(zahar);
            }

            Assert.True(a && boolList.All(j => j == true));
        }