C# 复杂排序问题(实体框架)

C# 复杂排序问题(实体框架),c#,asp.net,entity-framework,sql-order-by,C#,Asp.net,Entity Framework,Sql Order By,好的,我首先要说的是,我对所有这些东西都是新手,我会尽我最大的努力来完成这个项目。我有一个employee对象,它包含一个supervisor字段。当有人在我的页面上输入搜索时,datagrid会显示姓名与搜索匹配的员工。但是,我需要它显示向他们报告的所有员工以及向原始员工的下属报告的第三层员工。我只需要三层。为了简化这一点,员工只分为3个级别,因此如果级别==3,则该员工不负责其他人。我想从employee表中检索所有这些员工的最佳方法如下 from employee in context.e

好的,我首先要说的是,我对所有这些东西都是新手,我会尽我最大的努力来完成这个项目。我有一个employee对象,它包含一个supervisor字段。当有人在我的页面上输入搜索时,datagrid会显示姓名与搜索匹配的员工。但是,我需要它显示向他们报告的所有员工以及向原始员工的下属报告的第三层员工。我只需要三层。为了简化这一点,员工只分为3个级别,因此如果级别==3,则该员工不负责其他人。我想从employee表中检索所有这些员工的最佳方法如下

from employee in context.employees
where employee.name == search || employee.boss.name == search || 
employee.boss.boss.name == search
但我不知道如何使orderby以我想要的方式显示。我需要它分层显示。因此,它将看起来像: 大老板- 老板- 下属- 下属- 老板- 下属- 老板- 老板- 大老板-


就像我说的,可能有一种更简单的方法来处理整个问题,如果有,我洗耳恭听。非常感谢您提供的任何建议。

使用任何特定的ORM框架都很难解决这一问题,至少不需要一个简单的步骤。可能需要一个多步骤的过程

通过迭代搜索结果,找到他们的孩子(和孩子的孩子),并将层次结构扁平化为单个列表,可以实现大致相似的功能。这里是一个示例实现,该实现使用内存中的普通列表完成:

class Employee
{
    public int Id { get; private set; }
    public int? BossId { get; private set; }
    public string Name { get; private set; }

    public Employee(int id, int? bossId, string name)
    {
        Id = id;
        BossId = bossId;
        Name = name;
    }
}
样本数据:

List<Employee> employees = new List<Employee>();
employees.Add(new Employee(1, null, "Tom Smith"));
employees.Add(new Employee(2, null, "Susan Jones"));
employees.Add(new Employee(3, 1, "Sally Davis"));
employees.Add(new Employee(4, 1, "Robert Roberts"));
employees.Add(new Employee(5, 3, "John Smith"));
employees.Add(new Employee(6, 2, "Tonya Little"));
employees.Add(new Employee(7, 3, "Ty Bell"));
employees.Add(new Employee(8, 4, "Helen Andrews"));
employees.Add(new Employee(9, 2, "Matt Huang"));
employees.Add(new Employee(10, 6, "Lisa Wilson"));
结果:

1       Tom Smith
3       Sally Davis     1
5       John Smith      3
7       Ty Bell 3
4       Robert Roberts  1
8       Helen Andrews   4
5       John Smith      3
你可以看到它跟在最上面的结果后面,下属,下属的下属,下一个结果,任何下属,等等。它适用于任何级别


我不确定如何在Linq或常规SQL中通过“order by”实现这一点,但这只意味着我不够聪明,无法做到这一点。

我对Linq了解不多。但是,为什么不能使用EF模型可以引用的存储过程来实现呢

我认为,我们不应该事事都使用LINQ:)


编辑:我之所以说使用存储过程是有意义的,是因为EF必须生成SQL查询来做你想做的事情&你想做的事情,可以用SQL更好地表达和控制。

嘿,我想我会发布我解决这个问题的方法,以防它对其他人有用

var list = productQuery.ToList();
var productList = Functions.sortResultsList(list);




public static List<SolutionsModel.Version> 
   sortResultsList(List<SolutionsModel.Version> list)
{
    var productList = new List<SolutionsModel.Version>();

    int total = list.Count();
    int solutions = 0;
    int objects = 0;

    for (int length = 0; length < list.Count(); length++)
    {
        if (list[length].Product.TypeID == 1)
        {
            ++solutions;
        }
        else if (list[length].Product.TypeID == 2)
        {
            ++objects;
        }
    }

    //These nested for-loops create a list that is 
    //correctly ordered to fit correctly into the grid. 
    //Perhaps consider more efficient improvision at a later time.

    //These for loops can't be used if there are not any solutions 
    //in the results
    if (solutions != 0)
    {
        for (int x = 0; x < list.Count; x++)
        {
            if (list[x].Product.TypeID == 1)
            {
                productList.Add(list[x]);
                for (int y = 0; y < list.Count; y++)
                {
                    if (list[y].Product.TypeID != 1)
                    {
                        if (list[y].Product.Product2.ID == list[x].Product.ID && list[y].VersionNumber == list[x].VersionNumber)
                        {
                            productList.Add(list[y]);
                            for (int j = 0; j < list.Count; j++)
                            {
                                if (list[j].Product.TypeID == 3)
                                {
                                    if (list[j].Product.Product2.ID == list[y].Product.ID && list[j].VersionNumber == list[y].VersionNumber)
                                    {
                                        productList.Add(list[j]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //This can't be used if the results do not contain any objects.
    if (objects != 0 && productList.Count != total)
    {
        for (int y = 0; y < list.Count; y++)
        {
            if (list[y].Product.TypeID == 2)
            {
                productList.Add(list[y]);
                for (int j = 0; j < list.Count; j++)
                {
                    if (list[j].Product.TypeID == 3)
                    {
                        if (list[j].Product.Product2.ID == list[y].productID && list[j].VersionNumber == list[y].VersionNumber)
                        {
                            productList.Add(list[j]);
                        }
                    }
                }
            }
        }

    }

    //If the results contain only modules, no sorting is required and the original list can be used.
    if (productList.Count != total)
    {
          return list;
    }

    return productList;
}
var list=productQuery.ToList();
var productList=Functions.sortResultsList(列表);
公共静态列表
sortResultsList(列表)
{
var productList=新列表();
int total=list.Count();
int解=0;
int对象=0;
对于(int length=0;length
Wow!谢谢你花时间把这些写出来。我非常感激。我要到明天才能测试这个,但它看起来非常坚固。我对所有这些东西都不熟悉,所以我不知道如何用这种方式解决问题。再次感谢。祝你今天愉快@PFranchise,希望它能给你一个想法,我显然不希望它完全映射到你的数据模型。但只要稍作调整,你应该可以做类似的事情(如果没有更好的解决方案的话)。这是一个百万美元的问题,我恐怕目前只有四分之一美元的答案。考虑到这是搜索结果,而不仅仅是普通员工hie
1       Tom Smith
3       Sally Davis     1
5       John Smith      3
7       Ty Bell 3
4       Robert Roberts  1
8       Helen Andrews   4
5       John Smith      3
var list = productQuery.ToList();
var productList = Functions.sortResultsList(list);




public static List<SolutionsModel.Version> 
   sortResultsList(List<SolutionsModel.Version> list)
{
    var productList = new List<SolutionsModel.Version>();

    int total = list.Count();
    int solutions = 0;
    int objects = 0;

    for (int length = 0; length < list.Count(); length++)
    {
        if (list[length].Product.TypeID == 1)
        {
            ++solutions;
        }
        else if (list[length].Product.TypeID == 2)
        {
            ++objects;
        }
    }

    //These nested for-loops create a list that is 
    //correctly ordered to fit correctly into the grid. 
    //Perhaps consider more efficient improvision at a later time.

    //These for loops can't be used if there are not any solutions 
    //in the results
    if (solutions != 0)
    {
        for (int x = 0; x < list.Count; x++)
        {
            if (list[x].Product.TypeID == 1)
            {
                productList.Add(list[x]);
                for (int y = 0; y < list.Count; y++)
                {
                    if (list[y].Product.TypeID != 1)
                    {
                        if (list[y].Product.Product2.ID == list[x].Product.ID && list[y].VersionNumber == list[x].VersionNumber)
                        {
                            productList.Add(list[y]);
                            for (int j = 0; j < list.Count; j++)
                            {
                                if (list[j].Product.TypeID == 3)
                                {
                                    if (list[j].Product.Product2.ID == list[y].Product.ID && list[j].VersionNumber == list[y].VersionNumber)
                                    {
                                        productList.Add(list[j]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //This can't be used if the results do not contain any objects.
    if (objects != 0 && productList.Count != total)
    {
        for (int y = 0; y < list.Count; y++)
        {
            if (list[y].Product.TypeID == 2)
            {
                productList.Add(list[y]);
                for (int j = 0; j < list.Count; j++)
                {
                    if (list[j].Product.TypeID == 3)
                    {
                        if (list[j].Product.Product2.ID == list[y].productID && list[j].VersionNumber == list[y].VersionNumber)
                        {
                            productList.Add(list[j]);
                        }
                    }
                }
            }
        }

    }

    //If the results contain only modules, no sorting is required and the original list can be used.
    if (productList.Count != total)
    {
          return list;
    }

    return productList;
}