本文整理汇总了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);
}
示例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)));
}
示例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);
}
}
示例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());
}
示例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);
}
示例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));
}
示例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));
}
示例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);
}
示例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));
}
示例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());
}
示例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));
}
}