在电商产品模块中必经的一个环节是:当选择某一个产品类别,动态生成该类别下的所有属性和属性项,这些属性项有些是以DropDownList
的形式存在,有些是以CheckBoxList
的形式存在。接着,把CheckBoxList
的选中项组合生成产品SKU项。
本系列将在ASP.NET MVC中实现以上功能。但本篇,先在控制台实现属性值的笛卡尔乘积。
关于属性的类:
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; }}
通过几个方法模拟有关属性和属性项的数据。
static List<Prop> GetProps(){return new List<Prop>(){new Prop(){Id = 1, Name = "颜色"},new Prop(){Id = 2, Name = "尺寸"}};}static List<PropOption> GetPropOptions(){return new List<PropOption>(){new PropOption(){Id = 1, RealValue = "红色", PropId = 1},new PropOption(){Id = 2, RealValue = "蓝色", PropId = 1},new PropOption(){Id = 3, RealValue = "橙色", PropId = 1},new PropOption(){Id = 4, RealValue = "5英寸", PropId = 2},new PropOption(){Id = 5, RealValue = "8英寸", PropId = 2},new PropOption(){Id = 6, RealValue = "10英寸", PropId = 2},};}static string GetValueByPropOptionId(int id){return (GetPropOptions().Where(p => p.Id == id).FirstOrDefault()).RealValue;}
以上,
○ GetProps
方法获取所有属性
○ GetPropOptions
方法获取所有属性值
○ GetValueByPropOptionId
方法根据属性项的Id获取属性值
接下来,可能就是在前台勾选CheckBoxList
的项,把属性Id和对应的属性项Id封装成PropAndOption
类。
public class PropAndOption{public int PropId { get; set; }public int OptionId { get; set; }}
在服务端拿到的PropAndOption
对象集合假设是这样的:
//从前端获取的所有属性Id和属性项Idvar 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}};
接着,把List<PropAndOption>集合以PropId为分组标准,分成2组:
//根据属性Id分组,并得到属性值的分组var groupTempList = (from item in tempListgroup item by item.PropIdinto grpselect grp.Select(t => GetValueByPropOptionId(t.OptionId))).ToList();
于是,得到类似如下的结果:
组1:
红色
蓝色
橙色
组2:
5英寸
8英寸
10英寸
接着,把组1和组2进行笛卡尔乘积,我们的目的是想得到类似如下的string类型的集合:
红色 5英寸
红色 8英寸
红色 10英寸
以下声明一个string集合类型变量result
:
IEnumerable<string> result;result = groupTempList.First();groupTempList.RemoveAt(0);groupTempList.ForEach(delegate(IEnumerable<string> value){result = (from r in resultfrom v in valueselect r + " " + v).ToList();});
最后遍历result
这个字符串类型的集合。
foreach (var item in result){Console.WriteLine(item);}
完整的代码为:
using System;using System.Collections.Generic;using System.Linq;namespace ConsoleApplication1{class Program{static void Main(string[] args){//从前端获取的所有属性Id和属性项Idvar 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}};//根据属性Id分组,并得到属性值的分组var groupTempList = (from item in tempListgroup item by item.PropIdinto grpselect 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 resultfrom v in valueselect 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 = "颜色"},new Prop(){Id = 2, Name = "尺寸"}};}static List<PropOption> GetPropOptions(){return new List<PropOption>(){new PropOption(){Id = 1, RealValue = "红色", PropId = 1},new PropOption(){Id = 2, RealValue = "蓝色", PropId = 1},new PropOption(){Id = 3, RealValue = "橙色", PropId = 1},new PropOption(){Id = 4, RealValue = "5英寸", PropId = 2},new PropOption(){Id = 5, RealValue = "8英寸", PropId = 2},new PropOption(){Id = 6, RealValue = "10英寸", 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; }}}
运行。
如果在服务端只收到一个属性Id和属性项Id组成的PropAndOptio
对象。
var tempList = new List<PropAndOption>(){new PropAndOption(){PropId = 1, OptionId = 1}};
在下一篇,将在ASP.NET MVC中实现属性值的笛卡尔乘积。