亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - List<List<model>>如何更快捷的取里面的model?
PHP中文網(wǎng)
PHP中文網(wǎng) 2017-04-18 10:53:31
0
5
710
訪問接口返回數(shù)據(jù)類型為List<List<model>>,現(xiàn)在想將其中的model插入數(shù)據(jù)庫,感覺一點點循環(huán)有點傻,0.0...,各位有沒有其他的方法?
PHP中文網(wǎng)
PHP中文網(wǎng)

認證高級PHP講師

reply all(5)
洪濤

C# words:

var flat = list.SelectMany(l=>l).ToList();

Java words:

List<model> flat = list.stream().flatMap(List::stream).collect(Collectors.toList());
小葫蘆

list.stream().flatMap(model-> model.stream()).forEach(System.out::println);

小葫蘆

The data structure dictates, let’s loop

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;
                    }
                }
            }
        }
阿神

Don’t cycle yourself. Or catch other functions to help you complete the loop.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template