84669 personnes étudient
152542 personnes étudient
20005 personnes étudient
5487 personnes étudient
7821 personnes étudient
359900 personnes étudient
3350 personnes étudient
180660 personnes étudient
48569 personnes étudient
18603 personnes étudient
40936 personnes étudient
1549 personnes étudient
1183 personnes étudient
32909 personnes étudient
访问接口返回数据类型为List<List<model>>,现在想将其中的model插入数据库,感觉一点点循环有点傻,0.0...,各位有没有其他的方法?
认证高级PHP讲师
C# :
var flat = list.SelectMany(l=>l).ToList();
Pour Java :
List<model> flat = list.stream().flatMap(List::stream).collect(Collectors.toList());
list.stream().flatMap(model-> model.stream()).forEach(System.out::println);
La structure des données dicte, faisons une boucle
public static IEnumerable<T> GetItems<T>(this List<List<T>> list) { foreach (var child in list) { foreach (var item in child) { yield return item; } } } public static IEnumerable<T> GetNestItems<T>(this System.Collections.IList list) { Type type = null; foreach (var item in list) { if (type == null) type = item.GetType(); if (type == typeof(T)) { yield return (T)item; } else if (type.GetGenericTypeDefinition() == typeof(List<>)) { var items = GetNestItems<T>((System.Collections.IList)item); foreach (var t in items) { yield return t; } } } }
Ne faites pas de vélo vous-même. Ou capturez d'autres fonctions pour vous aider à boucler la boucle.
C# :
Pour Java :
list.stream().flatMap(model-> model.stream()).forEach(System.out::println);
La structure des données dicte, faisons une boucle
Ne faites pas de vélo vous-même. Ou capturez d'autres fonctions pour vous aider à boucler la boucle.