public void Enum_Convert_Into_Linq_Query() { var mapper = new BsonMapper(); var c = new Customer { Id = 1, Type = CustomerType.Loyal }; mapper.EnumAsInteger = true; var doc = mapper.ToDocument(c); doc["Type"].AsInt32.Should().Be(2); doc["Nullable"].IsNull.Should().BeTrue(); // To use Eum in LINQ expressions, Enum must be integer value (should be EnumAsInteger = true) var expr1 = mapper.GetExpression <Customer, bool>(x => x.Type == CustomerType.Loyal); expr1.Parameters["p0"].AsInt32.Should().Be(2); var expr2 = mapper.GetExpression <Customer, bool>(x => x.NullableType.Value == CustomerType.Loyal); expr2.Parameters["p0"].AsInt32.Should().Be(2); }
private void Eval <T, K>(T entity, Expression <Func <T, K> > expr, params K[] expect) { var expression = _mapper.GetExpression(expr); var doc = _mapper.ToDocument <T>(entity); var results = expression.Execute(doc).ToArray(); var index = 0; Assert.AreEqual(expect.Length, results.Length); foreach (var result in results) { var exp = _mapper.Serialize(typeof(K), expect[index++]); Assert.AreEqual(exp, result); } }
private BsonExpression Test <T, K>(Expression <Func <T, K> > expr, BsonExpression expect, params BsonValue[] args) { var expression = _mapper.GetExpression(expr); expression.Source.Should().Be(expect.Source); expression.Parameters.Keys.Count.Should().Be(args.Length, "Number of parameter are different than expected"); var index = 0; foreach (var par in args) { var pval = expression.Parameters["p" + (index++).ToString()]; pval.Should().Be(par, $"Expression: {expect.Source}"); } return(expression); }
private BsonExpression Test <T, K>(Expression <Func <T, K> > expr, BsonExpression expect, params BsonValue[] args) { var expression = _mapper.GetExpression(expr); Assert.AreEqual(expect.Source, expression.Source); Assert.AreEqual(expression.Parameters.Keys.Count, args.Length, "Number of parameter are different than expected"); var index = 0; foreach (var par in args) { var pval = expression.Parameters["p" + (index++).ToString()]; Assert.AreEqual(par, pval, "Expression: " + expect.Source); } return(expression); }