public void three_level_in_a_hierarchy_with_anonymous_types() { Expression <Func <Blog, object> > expression = x => new { x.Admin.Address.State }; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(1); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Admin.Address.State"); }
public void using_the_select_method_for_collections_with_anonymous_types() { Expression <Func <Blog, object> > expression = x => new { Prop = x.Posts.Select(p => p.Comments) }; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(1); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Posts.Comments"); }
public void simple_expression_with_anonymous_types() { Expression <Func <Blog, object> > expression = x => new { x.Admin }; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(1); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Admin"); }
public void two_level_in_a_hierarchy() { Expression <Func <Blog, object> > expression = x => x.Admin.Address; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(1); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Admin.Address"); }
public void using_the_select_method_for_sub_collections() { Expression <Func <Blog, object> > expression = x => x.Posts.SelectMany(p => p.Comments).Select(comment => comment.Member); var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(1); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Posts.Comments.Member"); }
public void should_support_multi_properties_using_the_select_method_for_sub_collections_with_anonymous_types() { Expression <Func <Blog, object> > expression = x => new { Prop = x.Name + x.Posts.SelectMany(p => p.Comments).Select(comment => comment.Member) }; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(2); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Name"); propertyPathVisitor.Properties.ElementAt(1).Should().Be("Posts.Comments.Member"); }
public void should_support_multi_properties_with_constants() { Expression <Func <Blog, object> > expression = x => "Name: " + x.Name + " " + x.Admin.Name; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(2); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Name"); propertyPathVisitor.Properties.ElementAt(1).Should().Be("Admin.Name"); }
public void should_support_multi_properties_with_anonymous_types() { Expression <Func <Blog, object> > expression = x => new { Prop = x.Name != x.Admin.Name }; var propertyPathVisitor = new PropertyPathVisitor(); propertyPathVisitor.Visit(expression); propertyPathVisitor.Properties.Should().HaveCount(2); propertyPathVisitor.Properties.ElementAt(0).Should().Be("Name"); propertyPathVisitor.Properties.ElementAt(1).Should().Be("Admin.Name"); }
public static string Property <TItem, TValue>(Expression <Func <TItem, TValue> > propertyExpression) { var path = PropertyPathVisitor.GetPath(propertyExpression); var memberInfo = path.Last(); if (!(memberInfo is PropertyInfo)) { throw new ArgumentException("The expression is for a method", nameof(propertyExpression)); } return(memberInfo.Name); }
public static string Property <T>(Expression <Func <T> > propertyExpression, bool allowNestedProperty = false) { var path = PropertyPathVisitor.GetPath(propertyExpression); if (path.Count > 1 && !allowNestedProperty) { throw new ArgumentException("Trying to get the name of a nested property: " + string.Join(".", path.Select(x => x.Name))); } var memberInfo = path.Last(); if (!(memberInfo is PropertyInfo)) { throw new ArgumentException("The expression is for a method", nameof(propertyExpression)); } return(memberInfo.Name); }
public static IQueryable <TEntity> Include <TEntity>(this IQueryable <TEntity> source, Expression <Func <TEntity, object> > selector) where TEntity : class { string path = new PropertyPathVisitor().GetPropertyPath(selector); return(source.Include(path)); //StringBuilder pathBuilder = new StringBuilder(); //MemberExpression pro = path.Body as MemberExpression; //while (pro != null) //{ // //如: x=> x.Customer.CustomerAddress // //path.Body是CustomerAddress // //CustomerAddress的Expression是Customer // //Customer的Expression是x // pathBuilder.Insert(0, "." + pro.Member.Name); // pro = pro.Expression as MemberExpression; //} //return source.Include(pathBuilder.ToString(1, pathBuilder.Length - 1)); }
public void complex_expression_with_anonymous_types() { Expression <Func <Foo, object> > selector1 = foo => new { foo.Name, foo.ImInAnEntity, foo.Bars, foo.UnitPrice, foo.UnitsInStock }; Expression <Func <Foo, object> > selector2 = foo => new { foo.Bar.Bee.Name, foo.Bar.ImInAnEntity, foo.Bar.Bee.UnitPrice }; var visitor = new PropertyPathVisitor(); visitor.Visit(selector1); visitor.Visit(selector2); var properties = visitor.Properties; properties.Should().HaveCount(8); properties.Where(x => x.Contains('.')).Should().HaveCount(3); properties.Where(x => !x.Contains('.')).Should().HaveCount(5); var expected = new HashSet <string> { "Name", "ImInAnEntity", "Bars", "UnitPrice", "UnitsInStock", "Bar.Bee.Name", "Bar.ImInAnEntity", "Bar.Bee.UnitPrice" }; for (var index = 0; index < properties.Count; index++) { properties.ElementAt(index).Should().Be(expected.ElementAt(index)); } properties.Should().Equal(expected); }
public static IQueryable <T> Include <T>(this IQueryable <T> query, Expression <Func <T, object> > selector) where T : class, new() { string path = new PropertyPathVisitor().GetPropertyPath(selector); return(query.Include(path)); }
public static ObjectQuery <T> Include <T>(this ObjectQuery <T> query, Expression <Func <T, object> > selector) { string path = new PropertyPathVisitor().GetPropertyPath(selector); return(query.Include(path)); }
public static ObjectQuery <TEntity> FetchMany <TEntity, TReleated>(this ObjectQuery <TEntity> query, Expression <Func <TEntity, IEnumerable <TReleated> > > selector) where TEntity : class { string path = new PropertyPathVisitor().GetPropertyPath(selector); return(query.Include(path)); }