コード例 #1
0
    public void Nullable()
    {
        var list = new List <TargetWithNullable>
        {
            new TargetWithNullable
            {
                Field = null
            },
            new TargetWithNullable
            {
                Field = 10
            }
        };

        var resultFromString = list
                               .Where(FuncBuilder <TargetWithNullable> .BuildPredicate("Field", Comparison.Equal, new[] { "10" }))
                               .Single();

        Assert.Equal(10, resultFromString.Field);
        var nullResult = list
                         .Where(FuncBuilder <TargetWithNullable> .BuildPredicate("Field", Comparison.Equal, null))
                         .Single();

        Assert.Null(nullResult.Field);
    }
コード例 #2
0
    static IEnumerable <TItem> ApplyToAll <TItem>(this IEnumerable <TItem> items, Func <Type, string, object> getArguments)
    {
        if (ArgumentReader.TryReadId(getArguments, out var values))
        {
            var predicate = FuncBuilder <TItem> .BuildPredicate("Id", Comparison.In, values);

            items = items.Where(predicate);
        }

        foreach (var where in ArgumentReader.ReadWhere(getArguments))
        {
            var predicate = FuncBuilder <TItem> .BuildPredicate(where);

            items = items.Where(predicate);
        }

        items = Order(items, getArguments);

        if (ArgumentReader.TryReadSkip(getArguments, out var skip))
        {
            items = items.Skip(skip);
        }

        if (ArgumentReader.TryReadTake(getArguments, out var take))
        {
            items = items.Take(take);
        }

        return(items);
    }
コード例 #3
0
    public void Nullable_requiring_parse()
    {
        var guid = Guid.Parse("00000000-0000-0000-0000-000000000001");
        var list = new List <TargetWithNullableRequiringParse>
        {
            new TargetWithNullableRequiringParse
            {
                Field = null
            },
            new TargetWithNullableRequiringParse
            {
                Field = guid
            }
        };

        var resultFromString = list
                               .Where(FuncBuilder <TargetWithNullableRequiringParse> .BuildPredicate("Field", Comparison.Equal, new[] { guid.ToString() }))
                               .Single();

        Assert.Equal(guid, resultFromString.Field);
        var nullResult = list
                         .Where(FuncBuilder <TargetWithNullableRequiringParse> .BuildPredicate("Field", Comparison.Equal, null))
                         .Single();

        Assert.Null(nullResult.Field);
    }
コード例 #4
0
    public void Field()
    {
        var list = new List <TargetWithField>
        {
            new TargetWithField
            {
                Field = "Target1"
            },
            new TargetWithField
            {
                Field = "Target2"
            }
        };

        var result = list
                     .Where(FuncBuilder <TargetWithField> .BuildPredicate("Field", Comparison.Equal, new[] { "Target2" }))
                     .Single();

        Assert.Equal("Target2", result.Field);
    }
コード例 #5
0
    public void NotInGuidList()
    {
        var list = new List <TargetForInGuid>
        {
            new TargetForInGuid
            {
                Member = Guid.Parse("00000000-0000-0000-0000-000000000001")
            },
            new TargetForInGuid
            {
                Member = Guid.Parse("00000000-0000-0000-0000-000000000002")
            }
        };

        var result = list
                     .Where(FuncBuilder <TargetForInGuid> .BuildPredicate("Member", Comparison.NotIn, new[] { "00000000-0000-0000-0000-000000000002" }))
                     .Single();

        Assert.Same(list[0], result);
    }
コード例 #6
0
    public void NotInList()
    {
        var list = new List <TargetForIn>
        {
            new TargetForIn
            {
                Member = "Value1"
            },
            new TargetForIn
            {
                Member = "Value2"
            }
        };

        var result = list
                     .Where(FuncBuilder <TargetForIn> .BuildPredicate("Member", Comparison.NotIn, new[] { "Value2" }))
                     .Single();

        Assert.Equal("Value1", result.Member);
    }
コード例 #7
0
    public void Nested()
    {
        var list = new List <Target>
        {
            new Target
            {
                Member = "a"
            },
            new Target
            {
                Member = "bb"
            }
        };

        var result = list
                     .Where(FuncBuilder <Target> .BuildPredicate("Member.Length", Comparison.Equal, new[] { "2" }))
                     .Single();

        Assert.Equal("bb", result.Member);
    }
コード例 #8
0
    public void NotInIntList()
    {
        var list = new List <TargetForInInt>
        {
            new TargetForInInt
            {
                Member = 1
            },
            new TargetForInInt
            {
                Member = 2
            }
        };

        var result = list
                     .Where(FuncBuilder <TargetForInInt> .BuildPredicate("Member", Comparison.NotIn, new[] { "2" }))
                     .Single();

        Assert.Equal(1, result.Member);
    }
コード例 #9
0
    public void Combos(string name, Comparison expression, string value, string expectedName, StringComparison?stringComparison)
    {
        var people = new List <Person>
        {
            new Person
            {
                Name        = "Person 1",
                Age         = 12,
                DateOfBirth = new DateTime(2001, 10, 10, 10, 10, 10, DateTimeKind.Utc)
            },
            new Person
            {
                Name        = "Person 2",
                Age         = 13,
                DateOfBirth = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc)
            }
        };

        var result = people
                     .Where(FuncBuilder <Person> .BuildPredicate(name, expression, new[] { value }, stringComparison))
                     .Single();

        Assert.Equal(expectedName, result.Name);
    }