java - List<List<model>>如何更快捷的取里面的model?
PHP中文网
PHP中文网 2017-04-18 10:53:31
0
5
587
访问接口返回数据类型为List<List<model>>,现在想将其中的model插入数据库,感觉一点点循环有点傻,0.0...,各位有没有其他的方法?
PHP中文网
PHP中文网

认证高级PHP讲师

répondre à tous(5)
洪涛

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

Peter_Zhu
        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.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!