更新分层编号

时间:2018-08-27 18:34:11

标签: c# mongodb dynamic hierarchical-data

我的应用程序中具有以下类,这些类包含如下层次数据:

Class A 
{
   string Id{get;set;}
   string Name{get;set;}
   string Order{get;set;}
   string ParentId{get;set;}
}
Class B 
{
   string Id{get;set;}
   string Name{get;set;}
   string Order{get;set;}
   string ClassAId{get;set;}
}
Class C 
{
   string Id{get;set;}
   string Name{get;set;}
   string Order{get;set;}
   string ClassBId{get;set;}
}

A类的样本数据为

Id = "1.1"                   Id = "2.1"
Name = "SomeName"            Name = "Name2"
Order = 1                    Order = 1
ParentId = "1"               ParentId = 2

Id = "1.2"                   Id = "2.2"
Name = "Name2"               Name = "Name3"
Order = 2                    Order = 2
ParentId = "1"               ParentId = 2

B类的样本数据应该是

 Id = "1.1.1"                   Id = "2.1.1"
    Name = "SomeName"            Name = "Name2"
    Order = 1                    Order = 1
    ParentId = "1.1"               ParentId = 2.1

    Id = "1.2.1"                   Id = "2.1.2"
    Name = "Name2"               Name = "Name3"
    Order = 2                    Order = 2
    ParentId = "1.2"               ParentId = 2.1

对于C类,数据看起来像

Id = "1.1.1.1"                   Id = "2.1.1.1"
Name = "SomeName"            Name = "Name2"
Order = 1                    Order = 1
ParentId = "1.1.1"               ParentId = 2.1.1

现在,如果用户要更新现有值之间的值,则它的工作方式是,假设我为A类输入的值介于1.2和1.3之间,那么它首先应创建一个名为1.4的新值,然后移动1.3及其子级的所有内容都为1.4(即,如果1.3拥有自己的子级,如1.3.1等,而1.3.1.1等则应分别重命名为1.4.1和1.4.1.1,并且1.3不应包含任何具有空值的层次结构。基本上在插入之间应该更新记录的完整层次结构。我能够通过找到当前ID并找到最大顺序并将其加1来正确生成下一个序列。我面临的问题是在插入之间并更新整个层次结构。 任何想法都会有所帮助。下面是我编写的用于输入新值Class A的代码:

//查找最大顺序,将其递增1并插入新记录。

var currentClass = listOfClassA.Where(x => x.Id = currentId).SingleOrDefault();
var maxOrder = listOfClassA.Max(x => x.Order);
var objClassA = new A();
objClassA.Order = maxOrder + 1;
objClassA.ParentId = currentClassA.ParentId;
objClassA.Name = "";
objClassA.Id = currentClassA.ParentId + "." + objClassA.Order;

2 个答案:

答案 0 :(得分:1)

根据您的要求只是一个想法,但将类包含到分层数据结构中会不会更容易,例如:

class A 
{
   public string Id{get;set;}
   public string Name{get;set;}
   public string Order{get;set;}
   //this is the tricky one.
   public string ParentId{get;set;}
}

class B 
{
   public string Id{get;set;}
   public string Name{get;set;}
   public string Order{get;set;}
   public A ClassA{get;set;}
}

class C 
{
   public string Id{get;set;}
   public string Name{get;set;}
   public string Order{get;set;}
   public B ClassB{get;set;}
}

另一个想法:由于您的类非常相似,因此您还可以创建一个真正的层次结构,如下所示:

public class Foo
{
    public string Id{get;set;}
    public string Name{get;set;}
    public string Order{get;set;}

    public Foo Parent {get;set;}
    public Foo Child{get;set;}
}


扩展先前的想法,您会发现您已经创建了一种LinkedList,例如:

public class Foo
{
    public string Id{get;set;}
    public string Name{get;set;}
    public string Order{get;set;}
}

var list = new LinkedList<Foo>();


还有一种选择:更像tree的结构,我相信它叫做composite pattern

public class Foo
{
    public string Id{get;set;}
    public string Name{get;set;}
    public string Order{get;set;}

    public Foo Parent {get;set;}
    public IEnumerable<Foo> Children{get;set;}
}

我希望它会有所帮助。如果需要,使用上述模式之一可以很容易地生成层次结构ID字符串,例如1.2.3.4.e.t.c.。因为将整个序列序列化也非常容易。提防循环引用;-)

使用SelectMany或递归调用来查找最大值或最小值的其他操作也很可行。

答案 1 :(得分:0)

我们决定将设计更改为使用动态expando对象,并使用它们生成层次结构编号(我们的要求是使用动态集合(mongodb)并动态生成它们)。尽管类结构将保持与上面相同。下面是我们为分层编号编写的代码。这段代码将Id作为输入(例如1.1或1.2或1.1.1或1.1.1.1)

                dynamic expando = new ExpandoObject();
                var collectionModel = expando as IDictionary<string, Object>;

                var lastDotPosition = value.LastIndexOf('.');
                var parentId = value.Substring(0, lastDotPosition);
                collectionModel.Add("ParentId", parentId);
                var order = Convert.ToInt32(value.Split('.').Last());


                var newOrder = order + 1;
                var Id = parentId + '.' + newOrder.ToString();

                collectionModel.Add("Index", newOrder);//newOrder
                collectionModel["Id"] = Id;

                var filter = Builders<dynamic>.Filter.Gt("Index", order);
                filter = filter & Builders<dynamic>.Filter.Eq("ParentId", parentId);
                var collection = _db.GetCollection<dynamic>(collectionName);

                var remainingList = collection.Find(filter).ToList();

                var dynamicList = new List<ExpandoObject>();
                dynamicList.Add((ExpandoObject)collectionModel);
                // below updates the next record ids and parent id to ensure numbering is maintained
                for (int i = 0; i < remainingList.Count; i++)
                {
                    var remainingRecord = remainingList[i] as IDictionary<string, object>;
                    var newRecord = new ExpandoObject() as IDictionary<string, Object>;
                    for(int j = 0; j < listOfFieldNames.Count; j++)
                    {
                        var fieldName = listOfFieldNames[j];
                        Object dictValue = "";
                        remainingRecord.TryGetValue(fieldName, out dictValue);
                        if (fieldName == "Id")
                        {
                            newRecord[fieldName] = parentId + "." + (newOrder + 1);
                        }
                        else
                        {
                            newRecord[fieldName] = dictValue;
                        }
                    }
                    newRecord["Index"] = newOrder + 1;
                    newRecord["ParentId"] = parentId;
                    newOrder++;

                    dynamicList.Add((ExpandoObject)newRecord);
                }


                //Now update child or grandchild if any

                var updateForChildList = remainingList.OrderByDescending(x => ((IDictionary<string, object>)x)["Index"]).ToList();

                for (int k = 0; k < updateForChildList.Count; k++)
                {
                    var oldRecord = updateForChildList[k] as IDictionary<string, object>;
                    var oldParentId = oldRecord["Id"];
                    Object dictValue = "";
                    oldRecord.TryGetValue("Index", out dictValue);

                    var newParentId = oldRecord["ParentId"] + "." + Convert.ToString(Convert.ToInt32(dictValue.ToString()) + 1);
                    UpdateParentIdForChildren(oldParentId.ToString(), newParentId, Convert.ToInt32(collectionOrder + 1));
                }
                collection.DeleteMany(filter);
                collection.InsertMany(dynamicList);

使用递归来查找子代和大子代,并更新其父代和ID

public void UpdateParentIdForChildren(string oldParentId, string newParentId, int collectionIndex)
        {


            if (collectionIndex > collectionList.Count)
            {
                return;
            }


            var currentCollection = _db.GetCollection<dynamic>(collectionName);
            var filter = Builders<dynamic>.Filter.Eq("ParentId", oldParentId);
            var oldParentIdList = currentCollection.Find(filter).ToList();
            var reoldParentIdList = oldParentIdList.OrderByDescending(x => ((IDictionary<string, object>)x)["Index"]).ToList();
            if (reoldParentIdList.Count > 0)
            {
                for (int i = 0; i < reoldParentIdList.Count; i++)
                {
                    var remainingRecord = reoldParentIdList[i] as IDictionary<string, object>;

                    Object OldIdValue = "";
                    remainingRecord.TryGetValue("Id", out OldIdValue);

                    Object indexValue = "";
                    remainingRecord.TryGetValue("Index", out indexValue);

                    var newId = newParentId + '.' + indexValue;

                    currentCollection.UpdateOne(filter, Builders<dynamic>.Update.Set("Id", newId));
                    currentCollection.UpdateOne(filter, Builders<dynamic>.Update.Set("ParentId", newParentId));

                    UpdateParentIdForChildren(OldIdValue.ToString(), newId, collectionIndex + 1);
                }
            }
        }