2017-01-04 95 views
0

你好我在使用.Any()擴展方法表達樹時遇到了問題。Linq表達式樹任何()問題

這是我的代碼:

IQueryable<Book> querableBooks = Books.Values.AsQueryable<Book>(); 

ParameterExpression pe = Expression.Parameter(typeof(Book), "book"); 

MemberExpression props = Expression.Property(pe, "properties"); 

ParameterExpression propList = Expression.Parameter(typeof(List<BookProperty>), "properties"); 

var _test = Expression.Lambda<Func<List<BookProperty>, bool>>(operation, new ParameterExpression[] { propList }); 

var _Any = Expression.Call(typeof(Enumerable), "any", new Type[] { typeof(BookProperty) }, new Expression[] { propList }); 

Expression Lamba = Expression.Lambda(_Any, _props); 

_test返回{屬性=>((bookProperty.type.key == 「內衣」)和(bookProperty.value == 「1」))}

_any返回{properties.Any()}

LAMBDA返回{book.properties => properties.Any()}

Book類是這樣的:

public class Book : IBook 
    { 

     public int id { get; set; } 
//Removed for clarity 
     public List<BookProperty> properties { get; set; } 

    } 

一個的BookProperty類:

public class BookProperty 
    { 
     public BookProperty() 
     { 
      value = "0"; 
     } 
     public int id { get; set; } 
     public int bookId { get; set; } 
     public Book book { get; set; } 
     public int typeId { get; set; } 
     public BookPropertyType type { get; set; } 
     public string value { get; set; } 
    } 

而且BookPropertyType類:

public class BookPropertyType 
    { 
     public int id { get; set; } 
     public string groupe { get; set; } 
     public string key { get; set; } 
     public string label { get; set; } 
     public int order { get; set; } 
     public string editorType { get; set; } 
     public string unite { get; set; } 
    } 

所以我接近它,但我不如何合併這一切正確的有像這樣的查詢

{book.propertie.Any(bookProperty => bookProperty.type.key ==「lingerie」)And(bookProperty.value ==「1」)}

感謝您的幫助。

+0

這將是有益的,如果你包括'Book'和'BookProperty'類。 –

+0

@IvanStoev我添加了類。謝謝 – Julien

+0

您是否正在尋找基於屬性類型和屬性值的Queryable書籍的子集?如果是這樣的話:queryableBooks.Where(book => book.properties.Any(property => property.type.key ==「lingerie」&& property.value ==「1」)) – Brino

回答

2

如果我理解正確,你正在尋找表達這樣的:

Expression<Func<Book, bool>> bookPredicate = book => book.properties.Any(
    bookProperty => bookProperty.type.key == "lingerie" && bookProperty.value == "1"); 

您可以動態地建立這樣的:

var book = Expression.Parameter(typeof(Book), "book"); 
var properties = Expression.PropertyOrField(book, "properties"); 
var bookProperty = Expression.Parameter(typeof(BookProperty), "bookProperty"); 
// bookProperty.type.key == "lingerie" 
var conditionA = Expression.Equal(
    Expression.PropertyOrField(Expression.PropertyOrField(bookProperty, "type"), "key"), 
    Expression.Constant("lingerie") 
); 
// bookProperty.value == "1" 
var conditionB = Expression.Equal(
    Expression.PropertyOrField(bookProperty, "value"), 
    Expression.Constant("1") 
); 
// bookProperty.type.key == "lingerie" && bookProperty.value == "1" 
var condition = Expression.AndAlso(conditionA, conditionB); 
// bookProperty => bookProperty.type.key == "lingerie" && bookProperty.value == "1" 
var predicate = Expression.Lambda<Func<BookProperty, bool>>(condition, bookProperty); 
// book.properties.Any(bookProperty => bookProperty.type.key == "lingerie" && bookProperty.value == "1") 
var anyCall = Expression.Call(
    typeof(Enumerable), "Any", new[] { typeof(BookProperty) }, 
    properties, predicate 
); 
// book => book.properties.Any(...) 
var bookPredicate = Expression.Lambda<Func<Book, bool>>(anyCall, book); 
+0

非常感謝您的幫助。 它工作正常。我能夠把它放在我已經完成的電話裏。它完美的作品。 我會清理一下我的代碼併發布最終結果。 – Julien