微信oauth2.0授权登录多久有效
更新时间:2022-04-30 09:04
最满意答案
我相信这会做你想要的。 在递归调用函数之前,您已声明了该函数。
// Declare the function so that it can be referenced from within // the function definition. Func<Node, object> convert = null; // Define the function. // Note the recursive call when setting the 'Children' property. convert = n => new { id = n.AssociatedObject.ID, name = n.AssociatedObject.Name, children = n.Children.Select(convert) }; // Convert the list of nodes to a list of the new type. var jsonTree = nodes .Select(convert) .ToList();
更新
通过在C#7中引入本地函数,您现在可以在函数中定义函数,就像通常定义函数和递归一样。
// Declare and define the function as you normally would. object convert (Node node) { id = n.AssociatedObject.ID, name = n.AssociatedObject.Name, children = n.Children.Select(convert); }; // Convert the list of nodes to a list of the new type. var jsonTree = nodes .Select(convert) .ToList();
I believe this will do what you want. Before you can call a function recursively you have declare the function.
// Declare the function so that it can be referenced from within // the function definition. Func<Node, object> convert = null; // Define the function. // Note the recursive call when setting the 'Children' property. convert = n => new { id = n.AssociatedObject.ID, name = n.AssociatedObject.Name, children = n.Children.Select(convert) }; // Convert the list of nodes to a list of the new type. var jsonTree = nodes .Select(convert) .ToList();
Update
With the introduction of local functions in C# 7 you can now define a function within a function as you would normally define a function and recursion simply works.
// Declare and define the function as you normally would. object convert (Node node) { id = n.AssociatedObject.ID, name = n.AssociatedObject.Name, children = n.Children.Select(convert); }; // Convert the list of nodes to a list of the new type. var jsonTree = nodes .Select(convert) .ToList();
相关问答
更多-
我相信这会做你想要的。 在递归调用函数之前,您已声明了该函数。 // Declare the function so that it can be referenced from within // the function definition. Func
convert = null; // Define the function. // Note the recursive call when setting the 'Children' property. conver ... -
听起来像你想要的 var movies = _db.Movies.Where(p => p.Genres.Intersect(listOfGenres)).Any(); 或者在Any提供谓词一次性完成所有操作: var movies = _db.Movies.Any(p => p.Genres.Intersect(listOfGenres)); Sounds like you want: var movies = _db.Movies.Where(p => p.Genres.Intersect(list ...
-
您可以使用SelectMany将它们全部作为平面列表获取: var groups = meta.SelectMany(m => m.Groups).ToList(); 这将为您提供List
。 SelectMany文档。 您可以在主List 上执行您希望的任何查询: var filteredGroups = meta .Where(m => m.IsActive) // Fictional "IsActive" boolean property. . ... -
您需要查看是否有任何标签名称与过滤器相同: books.Where(book => book.Tags.Any(tag => tag == genre)) 或使用Contains方法: books.Where(book => book.Tags.Contains(genre)) 假设Tags属性返回string 。 如果Tag是用户定义的对象,请使用: books.Where(book => book.Tags.Any(tag => tag.Name == genre)) you need to se ...
-
Linq在List
内重复给定范围的每个第n个元素(Linq repeat every nth element for a given range within a List [2022-08-30]) Linq用于查询 ,而不是更新 。 你可以编写有副作用的linq方法,但由于懒惰的枚举和其他因素,直接for或foreach循环是首选: int repeat = 2; int skip = 3; for(int i = 0; i < list.Count && repeat > 0; i += skip) { list[i].MyField = 1; repeat--; } Linq is for querying, not updating. You can write linq me ... -
如何使用LINQ从具有重复项的列表中获取第N个最大元素?(How do I get the Nth largest element from a list with duplicates, using LINQ?)[2023-06-22]
要使所有项目的集合等于第N个最大项目,您需要对项目进行分组,对组进行排序,然后按组大小递减N,同时N为正。 当N达到零时,您已经击中了包含第N个最大项目的组。 public static IEnumerableFoo (this IEnumerable source, int n) { return source.GroupBy(x => x) .OrderByDescending(group => group.Key) .SkipWhile(gro ... -
为什么save()不保存数据并且需要save(flush:true)?(why does save() not save the data and save(flush: true) is required?)[2022-01-18]
正如您可能想到的那样, save(flush: true)强制Hibernate将任何挂起的更改写入数据库。 如果没有显式刷新,则依赖于Hibernate事务在事务提交时自动刷新。 只有显式刷新对您有用的原因是因为您没有在事务中调用save() 。 最干净的解决方法是创建一个Grails服务,在其中放置nullifyBibNumbers() ,并使服务事务性 。 这将导致nullifyBibNumbers()被包装在一个事务中,这样你就可以使用save()而不需要显式刷新。 如果nullifyBibNumb ... -
int studentGrade = students .Where(d=>d.studentId == 1) .FirstOrDefault().studentCourses .FirstOrDefault(d=>d.courseId == 1).grade; int studentGrade = students .Where( ...
-
我明白了,父母可以包含多个项目:我相信这就是你要找的东西: var sortedList = from p in marketItems group p by p.ParentGroup into Groups select Groups; //to access the items in each group individually ...
-
您在查询上调用ToString - 这不是在单个元素上。 即使您修复了这个问题,也可以在列表中添加大量对同一对象的引用......这一切都可以通过LINQ更加清晰地完成: List
books = myDoc.Root.Elements("Book") .Select(x => new Book { Author = (string) x.Element("Author"), ...