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.DropDownList
In the formCheckBoxList
. NextCheckBoxList
To 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,
○GetProps
Method To obtain all attributes
○GetPropOptions
Method To obtain all attribute values
○GetValueByPropOptionId
Method to obtain the property value based on the property item Id
Next, you may selectCheckBoxList
To encapsulate the property Id and the corresponding property item IdPropAndOption
Class.
public class PropAndOption
{
public int PropId { get; set; }
public int OptionId { get; set; }
}
Obtained on the serverPropAndOption
The 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 Traversalresult
This 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 serverPropAndOptio
Object.
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.