我怎么能转换谓词< T>一个表达式来;谓语< T>>以起订量使用? [英] How can I convert a Predicate<T> to an Expression<Predicate<T>> to use with Moq?

查看:166
本文介绍了我怎么能转换谓词< T>一个表达式来;谓语< T>>以起订量使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助这个LINQ的新手!



我创建测试我的类中的名单,我想用起订量来检查结果。



我可以很容易地把一个谓语它检查列表的结果。 ?我怎么然后进行谓词入表达

  VAR myList中=新名单< INT> {1,2,3}; 

谓<名单,LT; INT>>比如说myPredicate =(名单)=>
{
返回list.Count == 3; //除其他的东西
};

// ...做我的东西

myMock.Verify(M = GT; m.DidStuffWith(It.Is<名单,LT; INT>>(??? )));



???需要有一个表达式来;谓语<名单< INT>>> 如果你能得到你的头一轮,许多仿制药。我发现答案这一轮做其他的方式和编译表达式成谓词。 。他们不帮助我了解LINQ的更好,虽然



编辑:我已经得到了它的工作方法;与表情;我只是想知道是否有任何方式与身体的lambda做到这一点 - 如果没有,为什么


解决方案
<? p>变化:

 谓词<名单,LT; INT>>比如说myPredicate =(名单)=> list.Count == 3; 

要:

 表达式来;谓语<名单,LT; INT>>>比如说myPredicate =(名单)=> list.Count == 3; 



该编译器的魔力为你。随着一些注意事项 1 ,只有处理任何拉姆达的表达式的(没有块)可以通过包装委托类型(在这种情况下,转换成一个表达式树谓词<名单,LT; INT>> )使用表达式来;> 。正如你提到的,然后你可以再次通过调用反其道而行 myPredicate.Compile()



1 例如,异步lambda表达式(即异步()=>等待Task.Delay(1); )不能转换为表达式树



更新:您只需不能的使用编译器在你想,如果它包括表达式树到达的语句的。你必须建立使用静态方法自己的表达式树(更多的工作)的表达式。 ( Expression.Block Expression.IfThen 等)


Please help this Linq newbie!

I'm creating a list inside my class under test, and I would like to use Moq to check the results.

I can easily put together a Predicate which checks the results of the list. How do I then make that Predicate into an Expression?

var myList = new List<int> {1, 2, 3};

Predicate<List<int>> myPredicate = (list) => 
                  {
                      return list.Count == 3; // amongst other stuff
                  };

// ... do my stuff

myMock.Verify(m => m.DidStuffWith(It.Is<List<int>>( ??? )));

??? needs to be an Expression<Predicate<List<int>>> if you can get your head round that many generics. I've found answers which do this the other way round and compile an Expression into a Predicate. They're not helping me understand Linq any better, though.

EDIT: I've got it working with methods; with expressions; I would just like to know if there's any way to do it with a lambda with a body - and if not, why not?

解决方案

Change:

Predicate<List<int>> myPredicate = (list) => list.Count == 3;

To:

Expression<Predicate<List<int>>> myPredicate = (list) => list.Count == 3;

The compiler does the magic for you. With some caveats1, any lambda dealing only with expressions (no blocks) can be converted into an expression tree by wrapping the delegate type (in this case Predicate<List<int>>) with Expression<>. As you noted, you could then invert it yet again by calling myPredicate.Compile().

1 For example, async lambdas (i.e. async () => await Task.Delay(1);) cannot be converted to an expression tree.

Update: You simply cannot use the compiler to arrive at the expression tree you want if it includes statements. You'll have to build up the expression tree yourself (a lot more work) using the static methods in Expression. (Expression.Block, Expression.IfThen, etc.)

这篇关于我怎么能转换谓词&LT; T&GT;一个表达式来;谓语&LT; T&GT;&GT;以起订量使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆