更新分层编号

问题描述 投票:-1回答:2

我的应用程序中有以下类,它包含如下的分层数据:

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来正确生成下一个序列。我面临的问题是在插入和更新之间任何想法都会有所帮助。以下是我为输入新值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;
c# mongodb dynamic hierarchical-data
2个回答
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;}
}


Expanding the previous idea, you'll find that you have created a sort of LinkedList, e.g.:
public class Foo
{
    public string Id{get;set;}
    public string Name{get;set;}
    public string Order{get;set;}
}

var list = new LinkedList<Foo>();


Yet another option: which is a more tree like structure, I believe it's called the 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或递归调用,查找最大值或最小值的其他操作也非常可行。


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

使用递归查找孩子和大孩子并更新他们的父母和孩子

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);
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.