如何为 2 个对象生成唯一 ID

how generate a unique id for 2 objects

我需要为每个循环为 2 个对象生成相同的 ID。我是否需要专门为 ID 创建另一个循环?一次创建的对象不会超过 20 个,因此不必担心碰撞。没有任何内容被保存到数据库中。

我需要为 productsId 和 Id 生成匹配的 uid

   public class data
{
    public int productsId { get; set; }
    public string sqft { get; set; }
    public string price { get; set; }

}
public class products
{
    public int Id { get; set; }
    public string product { get; set; }

}
public class LegendModel
{
    public string Color { get; set; }
    public string Name { get; set; }
    public IList<data> Data { get; set; }
    public IList<products> Products { get; set; }
}

public class ExportLegendController : ApiController
{

    // POST: api/ExportLegend
    [HttpPost]
    public PDF Post([FromBody]List<LegendModel> legendModel)
    {
        try
        {
            var subjectProperty = legendModel[legendModel.Count - 1];

            var xEleLegend = new XElement("Legend",
                        from item in legendModel
                        select new XElement("item",
                                     new XElement("Name", item.Name),
                                     new XElement("Color", item.Color)
                                   ));



            // Save the document...
            var dt = DateTime.Now.ToString("g").Replace('/', '-').Replace(':', '-');
            var filename = string.Format("{0}-{1}.xml", "Legend", dt);
            string physicalPath = HttpContext.Current.Server.MapPath("/legendXmls");
            string relativePath = Path.Combine(physicalPath, filename).Replace("\", "/");
            var pdfName = relativePath;

            xEleLegend.Save(pdfName);

            var data = new List<data>();
            var products = new List<products>();

            foreach (var item in subjectProperty.Data)
            {
                data.Add(new data
                {
                    productsId = item.productsId,
                    sqft = item.sqft,
                    price = item.price
                });
            }
            foreach (var item in subjectProperty.Products)
            {
                products.Add(new products
                {
                    Id = item.Id,
                    product = item.product
                });
            };

            var xEleProperty = new XElement("Property",
                         from d in data
                         join product in products on d.productsId equals product.Id
                         select new XElement("Points",
                                      new XElement("Sqft", d.sqft),
                                      new XElement("Price", d.price),
                                      new XElement("Product", product.product)
                                    ));

使用 GUID 和密码学

生成唯一 ID

使用 GUID:

public string generateID()
{
    return Guid.NewGuid().ToString("N");
}

"N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(32 位数字)

using System.Security.Cryptography; // Import this Dll
public string Get8Digits()
{
    var bytes = new byte[4];
    var rng = RandomNumberGenerator.Create();
    rng.GetBytes(bytes);
    uint random = BitConverter.ToUInt32(bytes, 0) % 100000000;
    return String.Format("{0:D8}", random);
}