Search code examples
c#asp.net-mvcmodel-view-controllerasp.net-web-apiuid

how generate a unique id for 2 objects


I need to generate identical ids for 2 objects for every loop. Do I need to make another loop specifcally for the ids? There wont be more than 20 objects created at a time so worrying about collisions isn't a big concern. nothing is being saved to a database.

I need to generate a matching uid for productsId and Id

   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)
                                    ));

Solution

  • Unique ID generation using GUID and Cryptography

    Using GUID:

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

    "N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits)

    Or

    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);
    }