func

How to extract properties used in a Expression<Func<T, TResult>> query and test their value?

冷暖自知 提交于 2019-12-12 19:12:09
问题 I need to create a function to evaluate queries for some rules before executing them. Here's the code: public class DataInfo { public int A { get; set; } public int B { get; set; } public int C { get; set; } } static class Program { static void Main() { var data = new DataInfo() { A = 10, B = 5, C = -1 }; // the result should be -1 int result = Calcul<DataInfo>(data, x => x.A / x.B + x.C); } static int Calcul<T>(T data, Expression<Func<T, int>> query) { // PSEUDO CODE // if one property used

Cast to Func vs new Func?

怎甘沉沦 提交于 2019-12-12 12:23:23
问题 Is there any difference between the following two statement? They both work. if ( ((Func<bool>)(()=>true))() ) { .... }; if ( new Func<bool>(()=>true)()) { .... }; 回答1: No, they both compile to exactly the same IL. It's easier to see if you actually give the lambda body something that depends on state - otherwise the compiler caches a single delegate instance for each lambda. But for example: using System; class Test { bool value = DateTime.Now.Hour == 10; void Cast() { if (((Func<bool>)(() =

Using FluentValidation's WithMessage method with a list of named parameters

五迷三道 提交于 2019-12-12 07:44:28
问题 I am using FluentValidation and I want to format a message with some of the object's properties value. The problem is I have very little experience with expressions and delegates in C#. FluentValidation already provides a way to do this with format arguments. RuleFor(x => x.Name).NotEmpty() .WithMessage("The name {1} is not valid for Id {0}", x => x.Id, x => x.Name); I would like to do something like this to avoid having to change the message string if I change the order of the parameters.

composing MEF parts in C# like a simple Funq container

冷暖自知 提交于 2019-12-12 03:48:15
问题 In Funq and probably most other IoC containers I can simply do this to configure a type: container.Register<ISomeThing>(c => new SomeThing()); How could I quickly extend MEF (or use existing MEF functionality) to do the same without using attributes. Here is how I thought I could do it: var container = new CompositionContainer(); var batch = new CompositionBatch(); batch.AddExport<ISomeThing>(() => new SomeThing()); batch.AddExportedValue(batch); container.Compose(batch); With this extension

Expression Tree Concatenation with LINQ to SQL

半世苍凉 提交于 2019-12-12 02:55:40
问题 I'm having a go at making a flexible exporter to export info from a SQL db accessed via LINQ to SQL, whereby the program can dynamically choose which fields to select and do all the processing server side. The final aim is to have a simple statement like: var result = db.Product.Select(p => selector(p)); Where selector is a dynamically created Expression Tree describing the fields to select. Currently, I have each of the db fields assigned to it's own individual selector like: Expression<Func

How to navigate as per MVVM architecture in Xamarin.Forms without using any frameworks like PRISM or any Navigation Service?

南笙酒味 提交于 2019-12-11 18:45:07
问题 I have a LoginPage.xaml that inherits PageBase.xaml . LoginViewModel is the ViewModel for LoginPage and it inherits BaseViewModel . BaseViewModel has Func delegate defined - OnModalNavigationRequest which is subscribed by PageBase in it's OnAppearing method and implementation is provided in HandleModalNavigationRequest method. When Login Button is clicked on LoginPage.xaml , OnSubmit method of LoginViewModel , bound through command is invoked which, on successful authentication, calls

Can I set a Func<> function with runtime variables to omit passing them as parameters in C#?

你离开我真会死。 提交于 2019-12-11 13:23:20
问题 I have a numerical analysis program which for simplicity calculates an algorithm similar to: y = ax^3 + bx^2 + cx + d; I calculate the values of a,b,c,d at runtime and would like to pass the following equivalent as a Func<double, double> . Where I can set a value for X, and get Y. y = 12x^3 + 13x^2 + 14x + 15; Where 12,13,14,15 are the numbers calculated at runtime. I realise this can be done by passing in a double array, like so: Func<double[], double> but I am trying to avoid passing around

Invoke Func<T1, T2, T3> which has optional parameters?

自闭症网瘾萝莉.ら 提交于 2019-12-11 11:03:34
问题 I have various methods which have two optional params, a ConfigSourceType enum and a string representing the filePath and return an instance of "IConfigFile" (Differs depending on the config file). I'm trying to pass these methods as Func so that I can load configs, but I can't invoke the delegate with no parameters. If I use Type.Missing it still complains about the ConfigSourceType enum being missing. Edit to include code: DataHandler class: public IConfigFile RetrieveMyConfiguration(

Providing a generic key comparison based on a collection of a generic type

孤街浪徒 提交于 2019-12-11 04:28:45
问题 I have created my own InsertOrUpdate() implementations for a few types like this: public IEnumerable<Genre> InsertOrUpdate(IEnumerable<Genre> genres) { foreach (var genre in genres) { var existingGenre = _context.Genres.SingleOrDefault(x => x.TmdbId == genre.TmdbId); if (existingGenre != null) { existingGenre.Update(genre); yield return existingGenre; } else { _context.Genres.Add(genre); yield return genre; } } _context.SaveChanges(); } The return type of IEnumerable<T> is required because it

How to work with Expression Trees not working using WCF since they are not serializable?

末鹿安然 提交于 2019-12-11 04:21:59
问题 I have a 4 layered architectured project, i.e. UserInterface, BusinessLogic, Service(WCF) and DataAccess(EF6) layers. I have exposed methods on my service that accept an expression which I can pass to my dataaccess layer to be evaluated using EF. However, this does not work because the Expression is not serializable. On my client end I want to be able to build queryable expressions to send to the server side and return the correct projection. Server Side: public virtual IEnumerable<Person>