当前位置: 首页>>代码示例>>C#>>正文


C# System.Collections.Select方法代码示例

本文整理汇总了C#中System.Collections.Select方法的典型用法代码示例。如果您正苦于以下问题:C# System.Collections.Select方法的具体用法?C# System.Collections.Select怎么用?C# System.Collections.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Collections的用法示例。


在下文中一共展示了System.Collections.Select方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VisitMethodCall

        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var instanceType = node.Object == null ? null : node.Object.Type;

            var map = new[] { new { Param = instanceType, Arg = node.Object } }.ToList();
            map.AddRange(node.Method.GetParameters()
                .Zip(node.Arguments, (p, a) => new { Param = p.ParameterType, Arg = a }));

            // for any local collection parameters in the method, make a
            // replacement argument which will print its elements
            var replacements = (map.Where(x => x.Param != null && x.Param.IsGenericType)
                .Select(x => new {x, g = x.Param.GetGenericTypeDefinition()})
                .Where(@t => @t.g == typeof (IEnumerable<>) || @t.g == typeof (List<>))
                .Where(@t => @t.x.Arg.NodeType == ExpressionType.Constant)
                .Select(@t => new {@t, elementType = @t.x.Param.GetGenericArguments().Single()})
                .Select(
                    @t =>
                        new
                        {
                            @[email protected],
                            Replacement =
                                Expression.Constant("{" + string.Join("|", (IEnumerable) ((ConstantExpression) @[email protected]).Value) + "}")
                        })).ToList();

            if (replacements.Any())
            {
                var args = map.Select(x => (from r in replacements
                                            where r.Arg == x.Arg
                                            select r.Replacement).SingleOrDefault() ?? x.Arg).ToList();

                node = node.Update(args.First(), args.Skip(1));
            }

            return base.VisitMethodCall(node);
        }
开发者ID:david0718,项目名称:LinqCache,代码行数:35,代码来源:LocalCollectionExpander.cs

示例2: SingleElementIndexedSelector

        public void SingleElementIndexedSelector()
        {
            var source = new[]
            {
                new  { name = "Prakash", custID = 98088 }
            };
            string[] expected = { "Prakash" };

            Assert.Equal(expected, source.Select((e, index) => e.name));
        }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:SelectTests.cs

示例3: SelectProperty

 public void SelectProperty()
 {
     var source = new[]{
         new { name="Prakash", custID=98088 },
         new { name="Bob", custID=29099 },
         new { name="Chris", custID=39033 },
         new { name=(string)null, custID=30349 },
         new { name="Prakash", custID=39030 }
     };
     string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" };
     Assert.Equal(expected, source.Select(e => e.name));
 }
开发者ID:dotnet,项目名称:corefx,代码行数:12,代码来源:SelectTests.cs

示例4: TryGetInstance_WithConcreteBclLists_ProducesLists

        public void TryGetInstance_WithConcreteBclLists_ProducesLists()
        {
            var resolver = new ListTypeResolver();
            var types = new[]
                            {
                                typeof (ArrayList),
                                typeof (List<>),
                                typeof (Collection<>)
                            };

            foreach (var name in types.Select(t => t.FullName)) {
                IList<object> list;
                Assert.IsTrue(resolver.TryGetListInstance(name, out list));
                Assert.NotNull(list);
            }
        }
开发者ID:benjamin-bader,项目名称:hessian,代码行数:16,代码来源:ListTypeResolverTests.cs

示例5: SelectWhere_Array_ReturnsExpectedValues

        public void SelectWhere_Array_ReturnsExpectedValues()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, bool> evenPredicate = (value) => value % 2 == 0;
            Func<int, int> addSelector = (value) => value + 1;

            IEnumerable<int> result = source.Select(addSelector).Where(evenPredicate);

            Assert.Equal(3, result.Count());
            Assert.Equal(2, result.ElementAt(0));
            Assert.Equal(4, result.ElementAt(1));
            Assert.Equal(6, result.ElementAt(2));
        }
开发者ID:chcosta,项目名称:corefx,代码行数:13,代码来源:WhereTests.cs

示例6: SelectPropertyUsingIndex

 public void SelectPropertyUsingIndex()
 {
     var source = new[]{
         new { name="Prakash", custID=98088 },
         new { name="Bob", custID=29099 },
         new { name="Chris", custID=39033 }
     };
     string[] expected = { "Prakash", null, null };
     Assert.Equal(expected, source.Select((e, i) => i == 0 ? e.name : null));
 }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:SelectTests.cs

示例7: Select_SourceIsArray_Count

 public void Select_SourceIsArray_Count()
 {
     var source = new[] { 1, 2, 3, 4 };
     Assert.Equal(source.Length, source.Select(i => i * 2).Count());
 }
开发者ID:dotnet,项目名称:corefx,代码行数:5,代码来源:SelectTests.cs

示例8: Select_ResetCalledOnEnumerator_ExceptionThrown

        public void Select_ResetCalledOnEnumerator_ExceptionThrown()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, int> selector = i => i + 1;

            var result = source.Select(selector);
            var enumerator = result.GetEnumerator();

            Assert.Throws<NotSupportedException>(() => enumerator.Reset());
        }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:SelectTests.cs

示例9: Select_GetEnumeratorCalledTwice_DifferentInstancesReturned

        public void Select_GetEnumeratorCalledTwice_DifferentInstancesReturned()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            var query = source.Select(i => i + 1);

            var enumerator1 = query.GetEnumerator();
            var enumerator2 = query.GetEnumerator();

            Assert.Same(query, enumerator1);
            Assert.NotSame(enumerator1, enumerator2);

            enumerator1.Dispose();
            enumerator2.Dispose();
        }
开发者ID:dotnet,项目名称:corefx,代码行数:14,代码来源:SelectTests.cs

示例10: Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIsCaught

        public void Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIsCaught()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, int> selector = i =>
            {
                if (i == 1)
                    throw new InvalidOperationException();
                return i + 1;
            };

            var result = source.Select(selector);
            var enumerator = result.GetEnumerator();

            Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
            enumerator.MoveNext();
            Assert.Equal(3 /* 2 + 1 */, enumerator.Current);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:17,代码来源:SelectTests.cs

示例11: Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller

        public void Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, int> selector = i => { throw new InvalidOperationException(); };

            var result = source.Select(selector);
            var enumerator = result.GetEnumerator();

            Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
        }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:SelectTests.cs

示例12: SelectSelect_SourceIsAnArray_ReturnsExpectedValues

        public void SelectSelect_SourceIsAnArray_ReturnsExpectedValues()
        {
            Func<int, int> selector = i => i + 1;
            int[] source = new[] { 1, 2, 3, 4, 5 };

            IEnumerable<int> query = source.Select(selector).Select(selector);

            int index = 0;
            foreach (var item in query)
            {
                var expected = selector(selector(source[index]));
                Assert.Equal(expected, item);
                index++;
            }

            Assert.Equal(source.Length, index);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:17,代码来源:SelectTests.cs

示例13: Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration

        public void Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration()
        {
            int[] source = new[] { 1 };
            Func<int, int> selector = i => i + 1;

            IEnumerable<int> query = source.Select(selector);

            var enumerator = query.GetEnumerator();
            while (enumerator.MoveNext()) ;

            Assert.Equal(default(int), enumerator.Current);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:12,代码来源:SelectTests.cs

示例14: SelectPropertyPassingIndexOnLast

 public void SelectPropertyPassingIndexOnLast()
 {
     var source = new[]{
         new { name="Prakash", custID=98088},
         new { name="Bob", custID=29099 },
         new { name="Chris", custID=39033 },
         new { name="Robert", custID=39033 },
         new { name="Allen", custID=39033 },
         new { name="Chuck", custID=39033 }
     };
     string[] expected = { null, null, null, null, null, "Chuck" };
     Assert.Equal(expected, source.Select((e, i) => i == 5 ? e.name : null));
 }
开发者ID:dotnet,项目名称:corefx,代码行数:13,代码来源:SelectTests.cs


注:本文中的System.Collections.Select方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。