我正在尝试生成基于productId和product categoryId的唯一产品系列或标签?
public string GetProductSerialNo(int productID,int categoryId)
{
var productSerialNo= (from a in db.AssetProductInfo
where a.ProductId == productID && a.CategoryId==categoryId
select a.ID).Max() + 1;
return productSerialNo.ToString();
}
如果表中至少有一个产品,则上述代码有效。有没有更好的解决方案来使用LINQ Query生成产品序列号。
提前致谢
答案 0 :(得分:0)
您可以添加类别名称的前两个字符,也可以添加此功能中的唯一编号,还可以添加毫秒的日期时间。
public string GetProductSerialNo(int productID,int categoryId,string CategoryFist2Character)
{
var productSerialNo= (from a in db.AssetProductInfo
where a.ProductId == productID && a.CategoryId==categoryId
select a.ID).Max() + UniqueNumber(CategoryFist2Character);
return productSerialNo.ToString();
}
public string UniqueNumber(string CategoryFist2Character)
{
Random unique1 = new Random();
string s = CategoryFist2Character;
int unique;
int n = 0;
while (n < 10)
{
if (n % 2 == 0)
{
s += unique1.Next(10).ToString();
}
else
{
unique = unique1.Next(52);
if (unique < this.characters.Length)
s = String.Concat(s, this.characters[unique]);
}
Label2.Text = s.ToString();
n++;
}
return s;
}