FAQ Database Discussion Community


Creating and Initializing a Generic Class

generics,func,object-initializers
How can I create and initialize the following class? Specifically I'm struggling with TSortKey. I thought it would be something like: var p = new QueryParameters<Employees, e => e.LastName>(); // WRONG!! public class QueryParameters<T, TSortKey> where T : class { public int Page { get; set; } public int PageSize...

Cannot invoke function in my main ViewController from subclass in Swift

swift,invoke,func
I've created a method call in my main ViewController called processLogin. The actual process is done in another class from GetRemoteData.swift as below public class GetRemoteData { class func getValidationFromServer(success: ((svrData: NSData!) -> Void)) { . . ViewController.processLogin() } The line which tries to invoke processLogin which is set out...

C# LINQ and func

c#,linq,lambda,func
This is a simplified version of what I am using, just to let you know of what I'm trying to accomplish: public Dictionary<int, Func<int>> Magic(Dictionary<int, int> dictionary) { return dictionary .Select(v => new { key = v.Key, function = new Func<int>(() => v.Value + 5) // * }) .ToDictionary(v =>...

Linq Query Expanding

c#,linq,delegates,func,anonymous-methods
I have a string array named Products & I am working on Anonymous Methods (Lambda Exprs.) & Linq. So first I wrote; var resultSet1 = from p in products where p.StartsWith("M") orderby p descending select p; Then I expand it with Lambda Expressions; var resultSet2 = products .Where(p => p.StartsWith("M"))...

NA output from sum of numbers in R

r,sum,na,func
I have a function and I get NA from sum function, the first sum works well, but the second sum does not work and returns NA. This is the function: Gramm.Pred.Err <- function(acts , grammProbs) { acts <- as.numeric(acts) grammProbs <- as.numeric(grammProbs) print("acts is:") print(acts) print("grammProbs is:") print(grammProbs) false.ind =...

golang “undefined” function declared in another file?

go,undefined,func
I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns: undefined: NewEmployee Here is the source code: main.go package main func main() { emp := NewEmployee() } employee.go package main type Employee struct {...

Add methods to Func

c#,list,func
I have a list of Func and I want to add elements. If I add them on Start like below, no problem: public List<System.Func<bool>> conditions = new List<System.Func<bool>>(); void Start() { conditions.Add(Iamdead); conditions.Add(Iamalive); } bool Iamdead() { ... return ...; } bool Iamalive() { ... return ...; } But I...

Dynamically adding properties to an object in c#

c#,object,func
I have a problem: public class PaginationSet { public int TotalItemCount { get; set; } public int Page { get; set; } public int Amount { get; set; } public string Sort { get; set; } public string Order { get; set; } /// <summary> /// This is used to...

What's the equivalent to not using params with Expression>[]?

entity-framework-6,func
I have some code in a repository base class for Entity Framework that eager loads Navigation properties: public virtual List<T> Find(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties) { //blah biddy blah blah } Then when calling the above method: var beers = BeerRepository.Find(x => x.Type == "IPA", a => a.Ingredients,...

Using delegate func as method parameter error

c#,func
private void btnTest_Click(object sender, EventArgs e) { //Func<Employee, int> getId = (x => x.EmployeeId); Func<TextBox, string> getInput = (x => x.Text); txtName.Text = GetInput(getInput); } private string GetInput<T>(Func<T, string> getInput) { string s = getInput(this.txtName.Text); return "Hello "+s; } At "string s = getInput(this.txtName.Text);" line i am facing the error...

Adding Properties into a List or Collection

c#,list,generics,func
I have come across a situation where I probably needed to add properties(of a class) in a list to invoke them manually(or you can say, I need to assign there values(setter)). That is why because, I don't even know which properties is to set the values, but they are decided...

Find the name of the invoked method of a Func delegate

c#,reflection,delegates,func
I have a simple invoker where, in order to be able to use a cache library , I need to know the name of the invoked method of an object that is a parameter of a Func delegate. class Program { static void Main(string[] args) { var proxy = new...

Method or delegate parameters do not match delegate parameters

c#,parameters,action,func
I'm doing some asynchronous caching and I'm using a simple Action callback to let the rest of the system know when the caching is complete. The entry point (the line causing the error): this.StartCoroutine<Action>(SceneUtils.CacheSceneNames, PopulateButtons); Coroutine method: public static IEnumerator CacheSceneNames(Action completedCallback) {} Callback method: private void PopulateButtons() {} And...

Are Func and Action Delegates in C# used by companies?

c#,events,delegates,action,func
I really wonder if the Func and Action delegates are used by developing companies. And do they use it for events? I know it's a strange question but I already asked it to my teacher and many other people but nobody could give me an answer.

Invoke Func without knowing its return type at compile time

c#,reflection,func
I'm trying to write something and I've hit a dead end. I have the following code : public class ObjectConverter { private Dictionary<Type, object> m_Conversions = new Dictionary<Type, object>(); public void AddConversion<TOut>(Func<object, TOut> conversion) { m_Conversions[typeof(TOut)] = conversion; } public T Convert<T>(object value) { var conversion = (Func<object, T>) m_Conversions[typeof(T)];...

Golang, how to return in func FROM another func?

go,return,func
I want to end execution in parent func apiEndpoint() upon calling/exiting in a child func apiResponse() func apiEndpoint() { if false { apiResponse("error") // I want apiResponse() call to return (end execution) in parent func // so next apiResponse("all good") wont be executed } apiResponse("all good") } func apiResponse(message string)...

how to fetch data from Func using generic list and linq

c#,linq,console-application,generic-list,func
I am new to C#. I have a class account with name and balance. I made a list of those accounts, and now what I'm trying to do is to somehow get all users whose balance is greater than 20000 using Func. I am aware of that my code ma...

Is it possible to invoke delegate from another delegate .net

c#,delegates,func
Hi I need your help please. I have the following code: public class CompensationProvider { private Func<List<Result_CompensationPolicy>, int> _EmployeeIdCount = (a => a.Where(t => !string.IsNullOrEmpty(t.EmployeeId)) .Select(x => x.EmployeeId) .Distinct() .Count()); private Func<List<Result_CompensationPolicy>, string, IEnumerable<HandlerCompanyCompensationSummery>> _GroupCompensationBalanceByHandler = ((compensationPolicy, managmentCompanyId) => (from p in compensationPolicy where...

Calling a Func From a Task

c#,task,func
I am writing a custom reporting tool that allows the users to possibly create some very broad queries. I wanted to add a timeout to this so if the user creates something that will end up running quite long, the entire system won't stall. I came up with this: public...

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

c#,linq,generics,expression-trees,func
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; } }...

Pass LINQ query to method to slect dynamically form collection

c#,linq,expression,func
I already looked up on the question on SO, but couldn't find a solution for my problem. In my program is a class whose sole reason is selecting from a collection of data passed to it via constructor. What I need to to is pass a LINQ query to the...

testing dynamically created strings

javascript,algorithm,comparison,func
what i have now is a function that return an array of 2 string that were put together using a random number generator. those values will help make up a string like "this is" + v1 + " and this is" + v2. v1 and v2 are the dynamically created...

MoveWindow arguments - c# Func<>

c#,dllimport,func
I am trying to 'export' a c# function for use with IronPython and python for .Net. I have the following: [DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); public Func<> Main() { return MoveWindow; } I am trying to pass the...