C# 如何按具有子/父关系的2个字段排序。对其进行排序,以便子项在排序列表中跟随其父项?

C# 如何按具有子/父关系的2个字段排序。对其进行排序,以便子项在排序列表中跟随其父项?,c#,linq,sql-order-by,icomparer,C#,Linq,Sql Order By,Icomparer,我在下表中有疑问 Id BeforeId Description 例如: Id BeforeId Description 1 NULL test 2 NULL test1 3 2 test2 4 3 test3 如果BeforeId不为null,则将在Id之前推送。我希望按Id和BeforeId按以下顺序排序 I

我在下表中有疑问

Id
BeforeId
Description
例如:

Id        BeforeId       Description
1         NULL           test
2         NULL           test1
3         2              test2
4         3              test3
如果BeforeId不为null,则将在Id之前推送。我希望按Id和BeforeId按以下顺序排序

Id        BeforeId       Description
1         NULL           test
4         3              test3
3         2              test2
2         NULL           test1
我正在尝试下面的代码,但它不是真的

var listOrder = _entites.Orders.OrderBy(t => t, new CustomComparer()).ToList();

 public class CustomComparer : IComparer<Order>
{
    public int Compare(Order lotA, Order lotB)
    {
        if (lotA.BeforeId!=null)
        {
            if (lotB.Id == lotA.BeforeId)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
        else if(lotB.BeforeId != null )
        {
            if(lotA.Id == lotB.BeforeId)
            {
                return 1; // A > B
            }
            else
            {
                return 0;
            }

        }
        else
        {
            return 0;
        }
    }
}
var listOrder=\u entites.Orders.OrderBy(t=>t,new CustomComparer()).ToList();
公共类CustomComparer:IComparer
{
公共整数比较(lotA订单、lotB订单)
{
if(lotA.BeforeId!=null)
{
if(lotB.Id==lotA.BeforeId)
{
返回-1;
}
其他的
{
返回0;
}
}
else if(lotB.BeforeId!=null)
{
if(lotA.Id==lotB.BeforeId)
{
返回1;//A>B
}
其他的
{
返回0;
}
}
其他的
{
返回0;
}
}
}
谁能告诉我如何解决这个问题


谢谢大家!

我创建了一个视图模型(add Seq column)并按如下方式对其排序

Id        BeforeId       Description   Seq
1         NULL           test           1
2         NULL           test1          2
3         2              test2          3
4         3              test3          4
我将自动生成序列号。之后,我将通过查找每个元素的beforeid来更新seq,以便再次对列表进行排序。大数据可能需要很多时间

//list order need to sort
var listNeedToSort = _entites.Order.ToList();
//list order have before id
var listBeforeId = listNeedToSort.Where(p=>p.BeforeId!=null).Select(p => p.BeforeId).ToList();
 //count number of duplicate data is not process
 int countLoopDuplicateButNotProcess = 0;

  while (listBeforeId.Any())
                {
                    foreach (var item in listNeedToSort.OrderByDescending(p => p.BeforeId))
                    {
                        if (item.BeforeId != null)
                        {
                            //get record which is mentioned by other record through beforeid.
                            var recordSummary = listNeedToSort.FirstOrDefault(p => p.Id == item.BeforeId);

                            if (recordSummary != null)
                            {
                                // if sequence number of item with before id greater than record which has id equals beforeid
                                if (item.Seq > recordSummary.Seq)
                                {
                                    //reset count loop but it process again
                                    countLoopDuplicateButNotProcess = 0;
                                    item.Seq = recordSummary.Seq;
                                    //sort again list
                                    foreach (var item1 in listNeedToSort.Where(p => p.Seq >= recordSummary.Seq && p.Id != item.Id).OrderBy(p => p.Seq))
                                    {
                                        item1.Seq += 1;
                                    }
                                    //remove beforeid in listBeforeId
                                    listBeforeId.Remove(item.BeforeId);
                                }
                                else
                                {
                                    //not process
                                    countLoopDuplicateButNotProcess += 1;
                                }
                            }
                            else
                            {
                                //reset count loop but it process again
                                countLoopDuplicateButNotProcess = 0;
                                  //remove beforeid in listBeforeId
                                listBeforeId.Remove(item.BeforeId);
                            }
                        }
                        else
                        {
                            //not process
                            countLoopDuplicateButNotProcess += 1;
                        }
                    }
                    //break if not process two times.
                    if (countLoopDuplicateButNotProcess == 2)
                    {
                        break;
                    }
                }

我理解你的问题:你试图对它进行排序,以便子项在排序列表中跟随它们的父项,其中关系由BeforeId/Id定义?我想在两个字段中使用复杂函数进行排序。但我不知道该怎么办?请注意,
Compare
应该返回-1、0或1,因为结果应该指示
lotA
相对于
lotB
应该位于哪里(在-1之前,相同的位置0,在1之后)。可能重复@David,这比这更复杂。