func

Concatenate two Func delegates

馋奶兔 提交于 2019-12-03 05:48:05
Assume that I have thes Class: public class Order { int OrderId {get; set;} string CustomerName {get; set;} } I declare below variables, too Func<Order, bool> predicate1 = t=>t.OrderId == 5 ; Func<Order, bool> predicate2 = t=>t.CustomerName == "Ali"; Is there any way that concatenate these variables(with AND/OR) and put the result in 3rd variable? for example: Func<Order, bool> predicate3 = predicate1 and predicate2; or Func<Order, bool> predicate3 = predicate1 or predicate2; And: Func<Order, bool> predicate3 = order => predicate1(order) && predicate2(order); Or: Func<Order, bool> predicate3 =

In few words, what can be said about Func<>

可紊 提交于 2019-12-03 02:38:39
I've been seing Func<> for sometime now, and I've manage to avoid it (for now). But, now it looks like I can't dodge it forever. For instance, I tried Dynamic Linq, but almost everything was in terms of Func<>. I've tried one of my book (C# 2008/Deitel&Deitel) and also MSDN but I'm not getting it yet. They all jump straight in the subject. What can be said (in few words) about Func<> Can I get some links on the web that can get me started on that matter? Thanks for helping Func<> is a generic delegate - it is just very convenient to use, because you don't have to create your own delegate for

Calling a method as a method parameter but not executing?

女生的网名这么多〃 提交于 2019-12-02 13:28:45
Hi I am trying to implement a method that will take a method (any method in the grand scheme of things) as a parameter. I want this parameter method to run when in the method that called it only, If the method that passes into this method has a return value then it should still be able to return its value. I want to measure the performance of the methods that are passed in. return Performance(GetNextPage(eEvent, false)); public static T Performance<T>(T method) { T toReturn; Stopwatch sw = Stopwatch.StartNew(); toReturn = method; sw.Stop(); Debug.WriteLine(sw.Elapsed.ToString()); return

Conversion from Func<object,string> to Func<string,string> works but to Func<int,string> fails

跟風遠走 提交于 2019-12-01 18:43:16
I have the following code: static Func<object, string> s_objToString = (x) => x.ToString(); static Func<string, string> s_stringToString = s_objToString; //compiles static Func<int, string> s_intToString = s_objToString; //error The second line compiles but the third line fails to compile with error: Cannot implicitly convert type ' System.Func<object,string> ' to ' System.Func<int,string> ' Why is that? I understand that with genetics although string is derived from object a List<string> does not derive from List<object> , but here object to string works and object to int fails, why? OK let's

How to moq a Func

跟風遠走 提交于 2019-12-01 15:15:29
问题 Trying to unit test a class whose constructor takes in a Func. Not sure how to mock it using Moq. public class FooBar { public FooBar(Func<IFooBarProxy> fooBarProxyFactory) { _fooBarProxyFactory = fooBarProxyFactory; } } [Test] public void A_Unit_Test() { var nope = new Mock<Func<IFooBarProxy>>(); var nope2 = new Func<Mock<IFooBarProxy>>(); var fooBar = new FooBar(nope.Object); var fooBar2 = new FooBar(nope2.Object); // what's the syntax??? } 回答1: figured it out public interface IFooBarProxy

Conversion of lambda expressions to Func

大兔子大兔子 提交于 2019-12-01 10:45:50
Given the following: open System.Linq let seqA = { 1..10 } this works: seqA.All (fun n -> n > 0) However this doesn't: let abc = fun n -> n > 0 seqA.All (abc) Why does F# offer implicit conversion from lambda expressions to Func s but not from functions? Pointers to the documentation where I can read up on what's going on here are welcome. :-) This is covered in the (rather involved) section of the spec on Method Resolution and again in Type-directed Conversions at Member Invocations . Quoting from the latter: As described in Method Application Resolution (see §14.4), two type-directed

Conversion of lambda expressions to Func

一曲冷凌霜 提交于 2019-12-01 08:38:57
问题 Given the following: open System.Linq let seqA = { 1..10 } this works: seqA.All (fun n -> n > 0) However this doesn't: let abc = fun n -> n > 0 seqA.All (abc) Why does F# offer implicit conversion from lambda expressions to Func s but not from functions? Pointers to the documentation where I can read up on what's going on here are welcome. :-) 回答1: This is covered in the (rather involved) section of the spec on Method Resolution and again in Type-directed Conversions at Member Invocations.

How do I create a generic Expression that has an expression as a parameter

我与影子孤独终老i 提交于 2019-12-01 00:49:42
There is a DisplayNameFor(x=>x.Title) helper in ASP.Net MVC. I want to implement something similar in behavior. I want to have a method that accepts an expression based on User class ( u=>u.Birthdate or u=>u.Name), a operand (Greater, Less, Equal) and a value like DateTime.Now and returns an expression u=>u.Birthdate > DateTime.Now I understand that I'll have to build resulting expression manually from pieces. What i can't wrap my head around is passing in and handling of property expression. Edit: I want to call a method like GetFilterPredicate(u=>u.Birthdate,FilterOps.GreaterThan,DateTime

Lambda\\Anonymous Function as a parameter

半世苍凉 提交于 2019-11-30 11:35:49
I'm a very new to C#. Just playing around with it. Not for a real purpose. 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; } ); } Is it possible to use a REAL anonymous functions (means returning result)? I do not want to use delegates such as // Somewhere in a code { Func<int> x = () => { return 0; }; makeOutput( x()) } Also I DO NOT want to change method parameter type such as void makeOutput( Func<int> _param) { } That is very common

Share expressions between Linq to Entities and Linq to Objects

心已入冬 提交于 2019-11-30 09:09:48
问题 I'm trying to "share" a set of conditions between a Linq to Entities call and a some other code, to reduce possible mismatches in conditions between the two calls. I started off by declaring my conditions: private Func<DateTime, Status, bool> _submissionDateExpiredCondition = (submissionDate, status) => submissionDate < DateTime.Now && status == Status.OK; private Func<DateTime, Status, bool> _submissionDateWithinOneWeekCondition = (submissionDate, status) => DateTime.Now < DbFunctions