c# - how generate a unique id for 2 objects -
i need generate identical ids 2 objects every loop. need make loop specifcally ids? there wont more 20 objects created @ time worrying collisions isn't big concern. nothing being saved database.
i need generate matching uid productsid , 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", item in legendmodel select new xelement("item", new xelement("name", item.name), new xelement("color", item.color) )); // save 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", 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) ));
unique id generation using guid , cryptography
using guid:
public string generateid() { return guid.newguid().tostring("n"); }
"n" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits)
or
using system.security.cryptography; // import 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); }
Comments
Post a Comment