Tag: func

Func 与委托和lambda表达式之间的区别

在深化自己更高级的C#function的同时,我遇到了一些代码,我并不完全知道它的区别。 这是关于这两行: Func giveLength = (text => text.Length); 和 Func giveLength = delegate(string text) { return text.Length; }; 这可以以相同的方式使用: Console.WriteLine(giveLength(“A random string.”)); 所以基本上……这两条线有什么区别? 这些行是否编译为相同的CIL?

在LINQ查询中使用Func

我有一个Func存储在CompareProductItemVendorIds 。 我想在LINQ查询中使用该表达式。 看来以下是合法的: var results = Repository.Query().Where(CompareProductItemVendorIds); 但是,以下内容不合法: var results = from v in Repository.Query() where CompareProductItemVendorIds(v) select v; 此代码产生错误: LINQ to Entities中不支持LINQ表达式节点类型“Invoke”。 问题: 为什么这些陈述如此不同以至于我的Func在一个而不是另一个合法? 我以为他们基本上都做了同样的事情。 我怎样才能做到这一点? 我是否必须明确地将我的Func创建为Expression<Func> ? 请在LINQ查询中的Using Expression <Func >中查看我的相关问题。

Lambda \ Anonymous函数作为参数

我是C#的新手。 只是玩弄它。 不是出于真正的目的。 void makeOutput( int _param) { Console.WriteLine( _param.ToString()); } //… // Somewhere in a code { makeOutput( /* some not c# code for an example for what do I want */ function : int () { return 0; } ); } 是否可以使用REAL匿名函数(意味着返回结果)? 我不想使用像这样的代表 // Somewhere in a code { Func x = () […]

为什么从表达式<Func >创建的Func 比直接声明的Func 慢?

为什么通过.Compile()从Expression<Func>创建的Func比使用直接声明的Func慢得多? 我刚刚使用Func直接声明为在我正在处理的应用程序中使用Expression<Func>创建的Expression<Func> ,我注意到性能下降了。 我刚做了一点测试,从Expression创建的Func占用了Func直接声明的时间的“几乎”。 在我的机器上,Direct Func大约需要7.5秒,而Expression<Func>大约需要12.6秒。 这是我使用的测试代码(运行Net 4.0) // Direct Func test1 = x => new Foo(x * 2); int counter1 = 0; Stopwatch s1 = new Stopwatch(); s1.Start(); for (int i = 0; i < 300000000; i++) { counter1 += test1(i).Value; } s1.Stop(); var result1 = s1.Elapsed; // Expression . Compile() Expression<Func> expression = […]

结合表达式树

我有以下表达式: public Expression<Func> UserAccessCheckExpression(int userId) where T : class { return x => (IsAdmin || userId == CurrentUserId || userId == 0); } 然后我想将此filter应用于几个集合(IQueryable),如下所示: return tasks .Where(t => t.TaskUsers .Any(x => UserAccessCheckExpression(x.User) && x.SomeBool == true)); 这样做时我收到以下错误: 错误40无法将类型System.Linq.Expressions.Expression<System.Func>隐式转换为bool 我不能使用接口inheritance的解决方法(比如TaskUserinheritance带有int UserId属性的接口(其中T:IHasUserId))因为我想要组合逻辑。

在Linq to Entities和Linq to Objects之间共享表达式

我试图在Linq to Entities调用和其他一些代码之间“共享”一组条件,以减少两个调用之间条件的可能不匹配。 我开始宣布我的条件: private Func _submissionDateExpiredCondition = (submissionDate, status) => submissionDate < DateTime.Now && status == Status.OK; private Func _submissionDateWithinOneWeekCondition = (submissionDate, status) => DateTime.Now < DbFunctions.AddDays(submissionDate, -7) && status == Status.Pending; private Func _bidValidityEndPeriodWithinThirtyDaysCondition = (bidValidityEndPeriod, status) => bidValidityEndPeriod.HasValue && DateTime.Now < DbFunctions.AddDays(bidValidityEndPeriod.Value, -30) && (status == Status.OK); 然后我想在我的where子句中使用这些条件,在Linq to Entities中调用和作为if语句中的函数(或者可能是Linq to Objects查询的where调用): […]

如何将System.Linq.Enumerable.WhereListIterator 转换为List ?

在下面的示例中,如何轻松地将eventScores转换为List以便我可以将其用作prettyPrint的参数? Console.WriteLine(“Example of LINQ’s Where:”); List scores = new List { 1,2,3,4,5,6,7,8 }; var evenScores = scores.Where(i => i % 2 == 0); Action<List, string> prettyPrint = (list, title) => { Console.WriteLine(“*** {0} ***”, title); list.ForEach(i => Console.WriteLine(i)); }; scores.ForEach(i => Console.WriteLine(i)); prettyPrint(scores, “The Scores:”); foreach (int score in evenScores) { Console.WriteLine(score); }

如何使用generics类型参数传入func?

我喜欢将generics类型转换器函数发送到方法,但我无法弄清楚如何做到这一点。 这是解释我想要实现的无效语法,问题是我不知道如何与我的func一起指定generics类型: public void SomeUtility(Func converter) { var myType = converter(“foo”); } 编辑(另请参阅我在Lawrence评论中的讨论):通过“generics转换器”我的意思是我想传入一个可以转换为任何强类型(不是对象)的转换器,所以我的下一行方法可能是: var myOtherType = converter(“foo”); 我想作为参数传递的委托看起来像这样: private delegate TOutput myConverterDelegate(object objectToConvert); 这更像是一种语法/ C#探索,为了完成任务,我可能会使用一个接口,但我希望这可以用func / delegate来完成。

如何使用out参数声明generics委托

Func ,只是不编译,如何声明我想要第二个参数是一个? 我想像这样使用它: public class Foo() { public Func DetectMethod; }