Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编写递归lambda表达式以获取所有相关数据的最佳方法是什么?_C#_Recursion_Linq_Lambda - Fatal编程技术网

C# 编写递归lambda表达式以获取所有相关数据的最佳方法是什么?

C# 编写递归lambda表达式以获取所有相关数据的最佳方法是什么?,c#,recursion,linq,lambda,C#,Recursion,Linq,Lambda,那么,让我们假设我有这样的表: +-----------------+-------------------+ | Parent Id | Child Id | |-----------------|-------------------| |1 |2 | |-----------------|-------------------| |1 |3

那么,让我们假设我有这样的表:

+-----------------+-------------------+ | Parent Id | Child Id | |-----------------|-------------------| |1 |2 | |-----------------|-------------------| |1 |3 | |-----------------|-------------------| |2 |88 | |-----------------|-------------------| |4 |5 | |-----------------|-------------------| |5 |6 | |-----------------|-------------------| |11 |12 | +-----------------+-------------------+ +-----------------+-------------------+ |父Id |子Id| |-----------------|-------------------| |1 |2 | |-----------------|-------------------| |1 |3 | |-----------------|-------------------| |2 |88 | |-----------------|-------------------| |4 |5 | |-----------------|-------------------| |5 |6 | |-----------------|-------------------| |11 |12 | +-----------------+-------------------+ 因此,基本上我想选择ParentId 1和所有相关的ID,这将是: 1,2,3,88

编写lambda表达式以获取所有相关数据的最佳方法是什么


我在想这件事似乎太乱了:

private void GetChildCategoryIds(List<CategoryEntity> categories, ref List<long> ids)
{
    foreach(var category in categories)
    {
        ids.Add( category.ChildId );
        GetChildCategoryIds( categories.Where( ... ), ref ids);
    }
}

foreach(var o in Data.Where( w => w.ParentId == 1 ))
{
    CategoryIds.Add( o.ChildId );
    GetChildCategoryIds( categories.Where( w => w.ParentId == o.ChildId ).ToList(), ref CategoryIds );
}
private void getChildCategoryId(列表类别,参考列表ID)
{
foreach(类别中的var类别)
{
id.Add(category.ChildId);
GetChildCategoryId(categories.Where(…),ref id);
}
}
foreach(Data.Where中的var o(w=>w.ParentId==1))
{
添加(o.ChildId);
getChildCategoryId(categories.Where(w=>w.ParentId==o.ChildId).ToList(),ref categoryId);
}

如果您绝对希望使用递归和lambda,那么下面的内容应该适用于小型数据集

    public static IEnumerable<Row> GetAllChildIds(int parentId, List<Row> data)
    {
        var parents = data.Where(x => x.Id == parentId).ToList();
        return parents.Concat(parents.SelectMany(x => GetAllChildIds(x.ChildId, data)));
    }
public静态IEnumerable GetAllChildId(int-parentId,列表数据)
{
var parents=data.Where(x=>x.Id==parentId.ToList();
返回parents.Concat(parents.SelectMany(x=>getAllChildId(x.ChildId,data));
}
下面是一个完整的工作示例:

class Program
{
    public static void Main()
    {
        var data = new Table {Rows = new List<Row>
            {
                new Row {Id = 1, ChildId = 2},
                new Row {Id = 1, ChildId = 3},
                new Row {Id = 2, ChildId = 88},
                new Row {Id = 4, ChildId = 5},
                new Row {Id = 5, ChildId = 6},
                new Row {Id = 11, ChildId = 12},
            }};

        var ids = GetAllChildIds(1, data.Rows).ToList();
    }

    public static IEnumerable<Row> GetAllChildIds(int parentId, List<Row> data)
    {
        var parents = data.Where(x => x.Id == parentId).ToList();
        return parents.Concat(parents.SelectMany(x => GetAllChildIds(x.ChildId, data)));
    }
}

class Row
{
    public int Id { get; set; }
    public int ChildId { get; set; }

    public override string ToString()
    {
        return Id + " -> " + ChildId;
    }
}

class Table
{
    public List<Row> Rows { get; set; }
}
类程序
{
公共静态void Main()
{
var数据=新表{行=新列表
{
新行{Id=1,ChildId=2},
新行{Id=1,ChildId=3},
新行{Id=2,ChildId=88},
新行{Id=4,ChildId=5},
新行{Id=5,ChildId=6},
新行{Id=11,ChildId=12},
}};
var id=getAllChildId(1,data.Rows).ToList();
}
公共静态IEnumerable GetAllChildId(int-parentId,列表数据)
{
var parents=data.Where(x=>x.Id==parentId.ToList();
返回parents.Concat(parents.SelectMany(x=>getAllChildId(x.ChildId,data));
}
}
班级排
{
公共int Id{get;set;}
public int ChildId{get;set;}
公共重写字符串ToString()
{
返回Id+“->”+ChildId;
}
}
类表
{
公共列表行{get;set;}
}

如果您绝对希望使用递归和lambda,那么下面的内容应该适用于小型数据集

    public static IEnumerable<Row> GetAllChildIds(int parentId, List<Row> data)
    {
        var parents = data.Where(x => x.Id == parentId).ToList();
        return parents.Concat(parents.SelectMany(x => GetAllChildIds(x.ChildId, data)));
    }
public静态IEnumerable GetAllChildId(int-parentId,列表数据)
{
var parents=data.Where(x=>x.Id==parentId.ToList();
返回parents.Concat(parents.SelectMany(x=>getAllChildId(x.ChildId,data));
}
下面是一个完整的工作示例:

class Program
{
    public static void Main()
    {
        var data = new Table {Rows = new List<Row>
            {
                new Row {Id = 1, ChildId = 2},
                new Row {Id = 1, ChildId = 3},
                new Row {Id = 2, ChildId = 88},
                new Row {Id = 4, ChildId = 5},
                new Row {Id = 5, ChildId = 6},
                new Row {Id = 11, ChildId = 12},
            }};

        var ids = GetAllChildIds(1, data.Rows).ToList();
    }

    public static IEnumerable<Row> GetAllChildIds(int parentId, List<Row> data)
    {
        var parents = data.Where(x => x.Id == parentId).ToList();
        return parents.Concat(parents.SelectMany(x => GetAllChildIds(x.ChildId, data)));
    }
}

class Row
{
    public int Id { get; set; }
    public int ChildId { get; set; }

    public override string ToString()
    {
        return Id + " -> " + ChildId;
    }
}

class Table
{
    public List<Row> Rows { get; set; }
}
类程序
{
公共静态void Main()
{
var数据=新表{行=新列表
{
新行{Id=1,ChildId=2},
新行{Id=1,ChildId=3},
新行{Id=2,ChildId=88},
新行{Id=4,ChildId=5},
新行{Id=5,ChildId=6},
新行{Id=11,ChildId=12},
}};
var id=getAllChildId(1,data.Rows).ToList();
}
公共静态IEnumerable GetAllChildId(int-parentId,列表数据)
{
var parents=data.Where(x=>x.Id==parentId.ToList();
返回parents.Concat(parents.SelectMany(x=>getAllChildId(x.ChildId,data));
}
}
班级排
{
公共int Id{get;set;}
public int ChildId{get;set;}
公共重写字符串ToString()
{
返回Id+“->”+ChildId;
}
}
类表
{
公共列表行{get;set;}
}

假设您有一个对象

public class Category
{
    public object SomeValue { get; set; }
    public int Id { get; set; }
    public int ParentId { get; set; }
}
您可以从
IEnumerable categories
获取
categoryId
的完整树,如下所示:

Func<Int, IEnumerable<Category>, IEnumerable<Category>> getChildren;
getChildren = (parentId, categories) =>
    categories
        .Where(cat => cat.ParentId == parentId)
        .SelectMany(cat => getChildren(cat.Id, categories).Concat(new Category[] { cat }));

getChildren(theParentCategoryId, yourListOfCategories);
Func-getChildren;
getChildren=(父ID,类别)=>
类别
.Where(cat=>cat.ParentId==ParentId)
.SelectMany(cat=>getChildren(cat.Id,categories).Concat(新类别[]{cat}));
getChildren(家长类别ID、您的类别列表);

还没有测试过,但看起来它应该在我的脑海中运行…

假设你有一个对象

public class Category
{
    public object SomeValue { get; set; }
    public int Id { get; set; }
    public int ParentId { get; set; }
}
您可以从
IEnumerable categories
获取
categoryId
的完整树,如下所示:

Func<Int, IEnumerable<Category>, IEnumerable<Category>> getChildren;
getChildren = (parentId, categories) =>
    categories
        .Where(cat => cat.ParentId == parentId)
        .SelectMany(cat => getChildren(cat.Id, categories).Concat(new Category[] { cat }));

getChildren(theParentCategoryId, yourListOfCategories);
Func-getChildren;
getChildren=(父ID,类别)=>
类别
.Where(cat=>cat.ParentId==ParentId)
.SelectMany(cat=>getChildren(cat.Id,categories).Concat(新类别[]{cat}));
getChildren(家长类别ID、您的类别列表);

我还没有测试过,但在我的脑海中似乎应该可以使用…

你问“什么是最好的方法”,但你尝试过的方法是什么?看起来你已经对必须做的事情有了很好的想法,但可能对一些细节不确定。你为什么不把你的问题告诉我们你已经想到了什么,哪怕只是一点伪代码?你会问“最好的方法是什么”,但你尝试过的方法是什么?看起来你已经对必须做的事情有了很好的想法,但可能对一些细节不确定。为什么不把你的问题告诉我们你已经想到了什么,哪怕只是一点伪代码呢?我在
getChildren
@karlingen的初始声明中使用了未赋值的局部变量“getChildren”@assign to
=null
我在
g的初始声明中使用了未赋值的局部变量“getChildren”
@karlingen