Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 从中获取一个类和一些数据';循环引用类中的父对象 请考虑以下代码: public class TimeSerie { public string Name { set; get; } public List<Data> ListData { get; set; } } public class Data { public int Id { get; set; } public TimeSerie A { get; set; } } public class ClassC { TimeSerie a; public ClassC() { a = new TimeSerie() { Name = "Nima", ListData = new List<Data>() { new Data() { Id = 1 , A = new TimeSerie() { Name = "MyName1", ListData = new List<Data>() { new Data() {Id= 1 }, new Data() {Id= 2 }, new Data() {Id= 3 }, new Data() {Id= 4 }, } } }, new Data() {Id= 2 }, new Data() {Id= 3 }, new Data() {Id= 4 }, new Data() {Id= 5 }, new Data() {Id= 18 }, new Data() { Id = 10, A = new TimeSerie() { Name = "MyName2", ListData = new List<Data>() { new Data() {Id= 5 }, new Data() { Id = 6, A = new TimeSerie() { Name="MyName3", ListData = new List<Data>() { new Data() {Id= 20 }, new Data() {Id= 2 }, new Data() {Id= 7 }, } } }, new Data() {Id= 7 }, new Data() {Id= 8 }, } } }, new Data() {Id= 20 }, new Data() {Id= 2 }, new Data() {Id= 7 }, new Data() {Id= 20 }, new Data() {Id= 15 }, } }; } }_C#_C# 4.0_C# 6.0 - Fatal编程技术网

C# 从中获取一个类和一些数据';循环引用类中的父对象 请考虑以下代码: public class TimeSerie { public string Name { set; get; } public List<Data> ListData { get; set; } } public class Data { public int Id { get; set; } public TimeSerie A { get; set; } } public class ClassC { TimeSerie a; public ClassC() { a = new TimeSerie() { Name = "Nima", ListData = new List<Data>() { new Data() { Id = 1 , A = new TimeSerie() { Name = "MyName1", ListData = new List<Data>() { new Data() {Id= 1 }, new Data() {Id= 2 }, new Data() {Id= 3 }, new Data() {Id= 4 }, } } }, new Data() {Id= 2 }, new Data() {Id= 3 }, new Data() {Id= 4 }, new Data() {Id= 5 }, new Data() {Id= 18 }, new Data() { Id = 10, A = new TimeSerie() { Name = "MyName2", ListData = new List<Data>() { new Data() {Id= 5 }, new Data() { Id = 6, A = new TimeSerie() { Name="MyName3", ListData = new List<Data>() { new Data() {Id= 20 }, new Data() {Id= 2 }, new Data() {Id= 7 }, } } }, new Data() {Id= 7 }, new Data() {Id= 8 }, } } }, new Data() {Id= 20 }, new Data() {Id= 2 }, new Data() {Id= 7 }, new Data() {Id= 20 }, new Data() {Id= 15 }, } }; } }

C# 从中获取一个类和一些数据';循环引用类中的父对象 请考虑以下代码: public class TimeSerie { public string Name { set; get; } public List<Data> ListData { get; set; } } public class Data { public int Id { get; set; } public TimeSerie A { get; set; } } public class ClassC { TimeSerie a; public ClassC() { a = new TimeSerie() { Name = "Nima", ListData = new List<Data>() { new Data() { Id = 1 , A = new TimeSerie() { Name = "MyName1", ListData = new List<Data>() { new Data() {Id= 1 }, new Data() {Id= 2 }, new Data() {Id= 3 }, new Data() {Id= 4 }, } } }, new Data() {Id= 2 }, new Data() {Id= 3 }, new Data() {Id= 4 }, new Data() {Id= 5 }, new Data() {Id= 18 }, new Data() { Id = 10, A = new TimeSerie() { Name = "MyName2", ListData = new List<Data>() { new Data() {Id= 5 }, new Data() { Id = 6, A = new TimeSerie() { Name="MyName3", ListData = new List<Data>() { new Data() {Id= 20 }, new Data() {Id= 2 }, new Data() {Id= 7 }, } } }, new Data() {Id= 7 }, new Data() {Id= 8 }, } } }, new Data() {Id= 20 }, new Data() {Id= 2 }, new Data() {Id= 7 }, new Data() {Id= 20 }, new Data() {Id= 15 }, } }; } },c#,c#-4.0,c#-6.0,C#,C# 4.0,C# 6.0,我怎样才能达到这个结果 谢谢您可以通过递归(即DFS搜索)来实现: 将这些帮助器方法添加到ClassC: public List<Dictionary<int, TimeSerie>> GetTimeSeries() { var ret = new List<Dictionary<int, TimeSerie>>(); if(a.ListData.Count

我怎样才能达到这个结果


谢谢

您可以通过递归(即DFS搜索)来实现:

将这些帮助器方法添加到
ClassC

 public List<Dictionary<int, TimeSerie>> GetTimeSeries()
            {
                var ret = new List<Dictionary<int, TimeSerie>>();

                if(a.ListData.Count != 0)
                {

                    foreach(var data in a.ListData)
                    {
                        GetRec(data, ref ret);
                    }
                }

                return ret;
            }

            private void GetRec(Data data, ref List<Dictionary<int, TimeSerie>> ret)
            {
                if(data.A != null && data.A.ListData.Count != 0)
                {
                    ret.Add(new Dictionary(data.id,data.A));
                    foreach(var d in data.A.ListData)
                    {
                        GetRec(d, ref ret);
                    }
                }   
            }

您可以使用递归(即DFS搜索)执行此操作:

将这些帮助器方法添加到
ClassC

 public List<Dictionary<int, TimeSerie>> GetTimeSeries()
            {
                var ret = new List<Dictionary<int, TimeSerie>>();

                if(a.ListData.Count != 0)
                {

                    foreach(var data in a.ListData)
                    {
                        GetRec(data, ref ret);
                    }
                }

                return ret;
            }

            private void GetRec(Data data, ref List<Dictionary<int, TimeSerie>> ret)
            {
                if(data.A != null && data.A.ListData.Count != 0)
                {
                    ret.Add(new Dictionary(data.id,data.A));
                    foreach(var d in data.A.ListData)
                    {
                        GetRec(d, ref ret);
                    }
                }   
            }

我创建了一个扩展方法,它使用递归选择直接与枚举数一起工作。请参见链接:

您可以将以下方法添加到ClassC中:

public IEnumerable<TimeSerie> GetFlattenedSeries()
{
    return a.GetDeepTimeSeries();
}
public IEnumerable GetFlattedSeries()
{
返回a.GetDeepTimeSeries();
}
请使用以下命令调用此命令:

ClassC c = new ClassC();
List<TimeSerie> myList = c.GetFlattenedSeries().ToList();
ClassC=newclassc();
List myList=c.GetFlattedSeries().ToList();
分机代码:

public static class EnumExtension
    {
        public static IEnumerable<TSource> RecursiveSelect<TSource>(this TSource source, Func<TSource, IEnumerable<TSource>> childSelector)
        {
            yield return source;
            var stack = new Stack<IEnumerator<TSource>>();
            var enumerator = childSelector(source).GetEnumerator();

            try
            {
                while (true)
                {
                    if (enumerator.MoveNext())
                    {
                        TSource element = enumerator.Current;
                        yield return element;

                        stack.Push(enumerator);
                        enumerator = childSelector(element).GetEnumerator();
                    }
                    else if (stack.Count > 0)
                    {
                        enumerator.Dispose();
                        enumerator = stack.Pop();
                    }
                    else
                    {
                        yield break;
                    }
                }
            }
            finally
            {
                enumerator.Dispose();

                while (stack.Count > 0) // Clean up in case of an exception.
                {
                    enumerator = stack.Pop();
                    enumerator.Dispose();
                }
            }
        }

        public static IEnumerable<TimeSerie> GetDeepTimeSeries(this TimeSerie serie)
        {
            return serie.RecursiveSelect<TimeSerie>(c=>c.ListData.Select(e=>e.A).Where(e=>e != null));
        }
    }
公共静态类枚举扩展
{
公共静态IEnumerable RecursiveSelect(此TSource源,Func childSelector)
{
收益回报源;
var stack=新堆栈();
var enumerator=childSelector(源).GetEnumerator();
尝试
{
while(true)
{
if(枚举数.MoveNext())
{
TSource元素=枚举数.Current;
收益-收益要素;
stack.Push(枚举器);
枚举器=子选择器(元素).GetEnumerator();
}
否则如果(stack.Count>0)
{
枚举数。Dispose();
枚举数=stack.Pop();
}
其他的
{
屈服断裂;
}
}
}
最后
{
枚举数。Dispose();
while(stack.Count>0)//在发生异常时进行清理。
{
枚举数=stack.Pop();
枚举数。Dispose();
}
}
}
公共静态IEnumerable GetDeepTimeSeries(此TimeSerie系列)
{
返回serie.RecursiveSelect(c=>c.ListData.Select(e=>e.A).Where(e=>e!=null));
}
}

我创建了一个扩展方法,它使用递归选择直接与枚举数一起工作。请参见链接:

您可以将以下方法添加到ClassC中:

public IEnumerable<TimeSerie> GetFlattenedSeries()
{
    return a.GetDeepTimeSeries();
}
public IEnumerable GetFlattedSeries()
{
返回a.GetDeepTimeSeries();
}
请使用以下命令调用此命令:

ClassC c = new ClassC();
List<TimeSerie> myList = c.GetFlattenedSeries().ToList();
ClassC=newclassc();
List myList=c.GetFlattedSeries().ToList();
分机代码:

public static class EnumExtension
    {
        public static IEnumerable<TSource> RecursiveSelect<TSource>(this TSource source, Func<TSource, IEnumerable<TSource>> childSelector)
        {
            yield return source;
            var stack = new Stack<IEnumerator<TSource>>();
            var enumerator = childSelector(source).GetEnumerator();

            try
            {
                while (true)
                {
                    if (enumerator.MoveNext())
                    {
                        TSource element = enumerator.Current;
                        yield return element;

                        stack.Push(enumerator);
                        enumerator = childSelector(element).GetEnumerator();
                    }
                    else if (stack.Count > 0)
                    {
                        enumerator.Dispose();
                        enumerator = stack.Pop();
                    }
                    else
                    {
                        yield break;
                    }
                }
            }
            finally
            {
                enumerator.Dispose();

                while (stack.Count > 0) // Clean up in case of an exception.
                {
                    enumerator = stack.Pop();
                    enumerator.Dispose();
                }
            }
        }

        public static IEnumerable<TimeSerie> GetDeepTimeSeries(this TimeSerie serie)
        {
            return serie.RecursiveSelect<TimeSerie>(c=>c.ListData.Select(e=>e.A).Where(e=>e != null));
        }
    }
公共静态类枚举扩展
{
公共静态IEnumerable RecursiveSelect(此TSource源,Func childSelector)
{
收益回报源;
var stack=新堆栈();
var enumerator=childSelector(源).GetEnumerator();
尝试
{
while(true)
{
if(枚举数.MoveNext())
{
TSource元素=枚举数.Current;
收益-收益要素;
stack.Push(枚举器);
枚举器=子选择器(元素).GetEnumerator();
}
否则如果(stack.Count>0)
{
枚举数。Dispose();
枚举数=stack.Pop();
}
其他的
{
屈服断裂;
}
}
}
最后
{
枚举数。Dispose();
while(stack.Count>0)//在发生异常时进行清理。
{
枚举数=stack.Pop();
枚举数。Dispose();
}
}
}
公共静态IEnumerable GetDeepTimeSeries(此TimeSerie系列)
{
返回serie.RecursiveSelect(c=>c.ListData.Select(e=>e.A).Where(e=>e!=null));
}
}

您想要一级孩子吗?还是将这种嵌套结构展平?@Rachmaninoff我想获取除根时间序列之外的具有其父级id的时间序列。结果
列表
表示其
A
的所有
数据
实例空?@skyoxZ是,除了rootSee你想要一级孩子?还是将这种嵌套结构展平?@Rachmaninoff我想获取除根时间序列之外的具有其父级id的时间序列。结果
列表
表示其
A
的所有
数据
实例null?@skyoxZ是,除了rootSee谢谢,但我想要类似字典的东西。我想要父齿逻辑的id相同,我编辑了代码以返回字典。Mer30 baradar damet garmThanks但我想要类似字典的东西。我想要父齿逻辑的id相同,我编辑了代码以返回字典。Mer30 baradar damet garm