ASP. net mvc implements the combination of attributes and attribute values, that is, the Cartesian Product 01. Implemented on the console, asp. netmvc

Source: Internet
Author: User

ASP. net mvc implements the combination of attributes and attribute values, that is, the Cartesian Product 01. Implemented on the console, asp. netmvc

An essential part of the E-commerce product module is: when a product category is selected, all attributes and attribute items under the category are dynamically generated.DropDownListIn the formCheckBoxList. NextCheckBoxListTo generate product SKU items.

 

This series will implement the above functions in ASP. net mvc. However, in this article, we first implement the Cartesian product of the attribute values on the console.

 

Attributes:

    public class Prop
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

 

Class About attribute items:

    public class PropOption
    {
        public int Id { get; set; }
Public string RealValue {get; set;} // store the attribute value
        public int PropId { get; set; }
    }    

 

Simulate data related to attributes and attribute items in several ways.

        static List<Prop> GetProps()
        {
            return new List<Prop>()
            {
New Prop () {Id = 1, Name = "color "},
New Prop () {Id = 2, Name = ""}
            };
        }
        static List<PropOption> GetPropOptions() 
        {
            return new List<PropOption>()
            {
New PropOption () {Id = 1, RealValue = "red", PropId = 1 },
New PropOption () {Id = 2, RealValue = "blue", PropId = 1 },
New PropOption () {Id = 3, RealValue = "orange", PropId = 1 },
New PropOption () {Id = 4, RealValue = "5 inch", PropId = 2 },
New PropOption () {Id = 5, RealValue = "8 inch", PropId = 2 },
New PropOption () {Id = 6, RealValue = "10 inch", PropId = 2 },
            };
        }
        static string GetValueByPropOptionId(int id)
        {
            return (GetPropOptions().Where(p => p.Id == id).FirstOrDefault()).RealValue;
        }

Above,
GetPropsMethod To obtain all attributes
GetPropOptionsMethod To obtain all attribute values
GetValueByPropOptionIdMethod to obtain the property value based on the property item Id

 

Next, you may selectCheckBoxListTo encapsulate the property Id and the corresponding property item IdPropAndOptionClass.

    public class PropAndOption
    {
        public int PropId { get; set; }
        public int OptionId { get; set; }
    }


Obtained on the serverPropAndOptionThe object set is assumed as follows:

// All attribute IDs and attribute item IDs obtained from the front-end
            var tempList = new List<PropAndOption>()
            {
                new PropAndOption(){PropId = 1, OptionId = 1},
                new PropAndOption(){PropId = 1, OptionId = 2},
                new PropAndOption(){PropId = 1, OptionId = 3},
                new PropAndOption(){PropId = 2, OptionId = 4},
                new PropAndOption(){PropId = 2, OptionId = 5},
                new PropAndOption(){PropId = 2, OptionId = 6}
            };

 

Next, divide the List <PropAndOption> set into two groups based on PropId:

// Group by attribute Id and obtain the group of attribute values
            var groupTempList = (from item in tempList
                group item by item.PropId
                into grp
                select grp.Select(t => GetValueByPropOptionId(t.OptionId))).ToList();

 

The following result is displayed:

 

Group 1:
Red
Blue
Orange

 

Group 2:
5 inch
8 inch
10 inch

 

Then, we multiply group 1 and group 2 by Descartes. Our goal is to get a set of string types similar to the following:

 

Red 5 inch
Red 8 inch
Red 10 inch

 

The following declaration is a string set type variableresult:

            IEnumerable<string> result;
            result = groupTempList.First();
            groupTempList.RemoveAt(0);
            groupTempList.ForEach(delegate(IEnumerable<string> value)
            {
                result = (from r in result
                    from v in value
                    select r + " " + v).ToList();
            });

 

Last TraversalresultThis string type set.

            foreach (var item in result)
            {
                Console.WriteLine(item);
            }

 

The complete code is:

using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
// All attribute IDs and attribute item IDs obtained from the front-end
            var tempList = new List<PropAndOption>()
            {
                new PropAndOption(){PropId = 1, OptionId = 1},
                new PropAndOption(){PropId = 1, OptionId = 2},
                new PropAndOption(){PropId = 1, OptionId = 3},
                new PropAndOption(){PropId = 2, OptionId = 4},
                new PropAndOption(){PropId = 2, OptionId = 5},
                new PropAndOption(){PropId = 2, OptionId = 6}
            };
// Group by attribute Id and obtain the group of attribute values
            var groupTempList = (from item in tempList
                group item by item.PropId
                into grp
                select grp.Select(t => GetValueByPropOptionId(t.OptionId))).ToList();
            IEnumerable<string> result;
            result = groupTempList.First();
            groupTempList.RemoveAt(0);
            groupTempList.ForEach(delegate(IEnumerable<string> value)
            {
                result = (from r in result
                    from v in value
                    select r + " " + v).ToList();
            });
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            
            Console.ReadKey();
        }
        static List<Prop> GetProps()
        {
            return new List<Prop>()
            {
New Prop () {Id = 1, Name = "color "},
New Prop () {Id = 2, Name = ""}
            };
        }
        static List<PropOption> GetPropOptions() 
        {
            return new List<PropOption>()
            {
New PropOption () {Id = 1, RealValue = "red", PropId = 1 },
New PropOption () {Id = 2, RealValue = "blue", PropId = 1 },
New PropOption () {Id = 3, RealValue = "orange", PropId = 1 },
New PropOption () {Id = 4, RealValue = "5 inch", PropId = 2 },
New PropOption () {Id = 5, RealValue = "8 inch", PropId = 2 },
New PropOption () {Id = 6, RealValue = "10 inch", PropId = 2 },
            };
        }
        static string GetValueByPropOptionId(int id)
        {
            return (GetPropOptions().Where(p => p.Id == id).FirstOrDefault()).RealValue;
        }
    }
    public class Prop
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class PropOption
    {
        public int Id { get; set; }
        public string RealValue { get; set; }
        public int PropId { get; set; }
    }
    public class PropAndOption
    {
        public int PropId { get; set; }
        public int OptionId { get; set; }
    }
}

Run.

 

If only one property Id and one property item Id are received on the serverPropAndOptioObject.

            var tempList = new List<PropAndOption>()
            {
                new PropAndOption(){PropId = 1, OptionId = 1}
            };

In the next article, the Cartesian product of the attribute value will be implemented in ASP. net mvc.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.