【问题标题】:how generate a unique id for 2 objects如何为 2 个对象生成唯一 ID
【发布时间】:2015-07-05 19:11:54
【问题描述】:

我需要为每个循环为 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)
                                    ));

【问题讨论】:

  • 不确定我是否理解该评论。如果我不清楚我的问题,请询问
  • 你能举一个你想要完成的例子吗? “唯一”的意思是“一种”,似乎不会被分配给 2 个对象。
  • @ScottHunter 刚刚发布了我想要完成的内容;ish
  • 我需要使用 ids 来防止我的 xml 文档中出现重复元素。

标签: c# asp.net-mvc model-view-controller asp.net-web-api uid


【解决方案1】:

使用 GUID 和密码学生成唯一 ID

使用 GUID:

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

“N” - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(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);
}

【讨论】:

  • 原谅我的无知,我如何使用第一个。
  • @texas697 你打电话给generateID()
猜你喜欢
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多