相关疑难解决方法(0)

组合两个表达式(Expression <Func <T,bool >>)

我有两个类型的表达式,Expression<Func<T, bool>>我想采取OR,AND或NOT这些并得到一个相同类型的新表达式

Expression<Func<T, bool>> expr1;
Expression<Func<T, bool>> expr2;

...

//how to do this (the code below will obviously not work)
Expression<Func<T, bool>> andExpression = expr AND expr2
Run Code Online (Sandbox Code Playgroud)

c# linq lambda expression

232
推荐指数
5
解决办法
9万
查看次数

将表达式<Func <IBar,T >>转换为表达式<Func <Bar,T >>

你如何转换Expression<Func<IBar, T>>Expression<Func<Bar, T>>Bar实施IBar?

这个更通用的问题有答案:

将表达式<Func <T1,bool >>动态转换为Expression <Func <T2,bool>

这是这种情况下最好的方法吗?鉴于Bar实施IBar,有没有更简单的方法?

所以,鉴于这个人为的示例代码:

          public class Foo<T>
          {
                 private readonly List<T> _list = new List<T>();

                 public void Add(T item)
                 {
                       _list.Add(item);
                 }

                 public bool AnyFunc(Func<T, bool> predicate)
                 {
                       return _list.Any(predicate);
                 }

                 public bool AnyExpression(Expression<Func<T, bool>> expression)
                 {
                       return _list.AsQueryable().Any(expression);
                 }                    
          }

          public interface IBar
          {
                 string Name { get; }
          }

          public class Bar : IBar
          {
                 public string Name { get; set; }
          }
Run Code Online (Sandbox Code Playgroud)

这证明了这个问题:

          public class Test() …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda

3
推荐指数
1
解决办法
133
查看次数

标签 统计

c# ×2

lambda ×2

linq ×2

expression ×1