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


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

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


在下文中一共展示了System.Collections.Where方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: CreateImpedimentData

 public string CreateImpedimentData(bool? isPrivate, int? ownerId, int? responsibleId)
 {
     var parts = new[]
         {
             isPrivate.GetValueOrDefault() ? string.Empty : "public",
             ownerId.HasValue ? EncodeStringId(ownerId.Value, "Owner") : string.Empty,
             responsibleId.HasValue ? EncodeStringId(responsibleId.Value, "Responsible") : string.Empty
         };
     return string.Join(" ", parts.Where(x => !string.IsNullOrEmpty(x)));
 }
开发者ID:chenhualei,项目名称:Target-Process-Plugins,代码行数:10,代码来源:IndexDataFactory.cs

示例3: Where_GetEnumeratorReturnsUniqueInstances

        public void Where_GetEnumeratorReturnsUniqueInstances()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };

            var result = source.Where(value => true);

            using (var enumerator1 = result.GetEnumerator())
            using (var enumerator2 = result.GetEnumerator())
            {
                Assert.Same(result, enumerator1);
                Assert.NotSame(enumerator1, enumerator2);
            }
        }
开发者ID:chcosta,项目名称:corefx,代码行数:13,代码来源:WhereTests.cs

示例4: Select_SourceThrowsOnReset

        public void Select_SourceThrowsOnReset()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };

            var enumerator = source.Where(value => true).GetEnumerator();

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

示例5: Where_PredicateThrowsException

        public void Where_PredicateThrowsException()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, bool> predicate = value =>
            {
                if (value == 1)
                {
                    throw new InvalidOperationException();
                }
                return true;
            };

            var enumerator = source.Where(predicate).GetEnumerator();

            // Ensure the first MoveNext call throws an exception
            Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());

            // Ensure Current is set to the default value of type T
            int currentValue = enumerator.Current;
            Assert.Equal(default(int), currentValue);

            // Ensure subsequent MoveNext calls succeed
            Assert.True(enumerator.MoveNext());
            Assert.Equal(2, enumerator.Current);
        }
开发者ID:chcosta,项目名称:corefx,代码行数:25,代码来源:WhereTests.cs

示例6: WhereSelectSelect_Array_ReturnsExpectedValues

        public void WhereSelectSelect_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.Where(evenPredicate).Select(i => i).Select(addSelector);

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

示例7: WhereWhere_Array_ReturnsExpectedValues

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

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

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

示例8: Where_Array_CurrentIsDefaultOfTAfterEnumeration

        public void Where_Array_CurrentIsDefaultOfTAfterEnumeration()
        {
            int[] source = new[] { 1 };
            Func<int, bool> truePredicate = (value) => true;

            var enumerator = source.Where(truePredicate).GetEnumerator();
            while (enumerator.MoveNext()) ;

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

示例9: Where_Array_ReturnsExpectedValues_Complex

        public void Where_Array_ReturnsExpectedValues_Complex()
        {
            int[] source = new[] { 2, 1, 3, 5, 4 };
            Func<int, int, bool> complexPredicate = (value, index) => { return (value == index); };

            IEnumerable<int> result = source.Where(complexPredicate);

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

示例10: Where_Array_ReturnsExpectedValues_False

        public void Where_Array_ReturnsExpectedValues_False()
        {            
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, bool> falsePredicate = (value) => false;

            IEnumerable<int> result = source.Where(falsePredicate);

            Assert.Equal(0, result.Count());
        }
开发者ID:chcosta,项目名称:corefx,代码行数:9,代码来源:WhereTests.cs

示例11: Where_Array_ReturnsExpectedValues_True

        public void Where_Array_ReturnsExpectedValues_True()
        {
            int[] source = new[] { 1, 2, 3, 4, 5 };
            Func<int, bool> truePredicate = (value) => true;

            IEnumerable<int> result = source.Where(truePredicate);

            Assert.Equal(source.Length, result.Count());
            for (int i = 0; i < source.Length; i++)
            {
                Assert.Equal(source.ElementAt(i), result.ElementAt(i));
            }
        }
开发者ID:chcosta,项目名称:corefx,代码行数:13,代码来源:WhereTests.cs


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