您好,我在使用.Any()扩展方法的表达式树时遇到麻烦。

这是我的代码:

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 ==“内衣”)和(bookProperty.value ==“ 1”)}

谢谢你的帮助。

最佳答案

如果我理解正确,那么您正在寻找这样的表达方式:

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);

关于c# - Linq表达式树Any()问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41470086/

10-11 20:45