你好我在使用.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」)}
感謝您的幫助。
這將是有益的,如果你包括'Book'和'BookProperty'類。 –
@IvanStoev我添加了類。謝謝 – Julien
您是否正在尋找基於屬性類型和屬性值的Queryable書籍的子集?如果是這樣的話:queryableBooks.Where(book => book.properties.Any(property => property.type.key ==「lingerie」&& property.value ==「1」)) – Brino