func

A delegate for a function with variable parameters

冷暖自知 提交于 2019-12-08 15:30:03
问题 I have a function of this sort void func(params object[] parameters) { //Function Body } It can accept parameters of the following sort func(10, "hello", 30.0); func(10,20); and so on. I wanted to create an Action delegate for the above function. Is it possible? If not then why? 回答1: You can't use the existing Action delegates with params , but you can declare your own delegate that way: public delegate void ParamsAction(params object[] arguments) Then: // Note that this doesn't have to have

Casting/Mapping Delegates

淺唱寂寞╮ 提交于 2019-12-08 09:50:51
问题 I have a method public List<DTO.User> GetUsers(Func<Domain.User, bool> expression) { var users = new List<DTO.User>(); using(UserContext context = new UserContext()) { // obviously an error users = context.Users.ToList(); } return users; } Notice the DTO.User (a DTO) and Domain.User (a domain entity from EF) So I use AutoMapper to map entities like this public List<DTO.User> GetUsers() { var users = new List<DTO.User>(); using(UserContext context = new UserContext()) { Mapper.CreateMap<Domain

Issues with Generics using Silverlight

三世轮回 提交于 2019-12-08 08:32:36
问题 I am creating a C# web application using Silverlight 5 (VS 2010). I initially created a console application which works fine and now i am adapting it into a web app. Even in web application it is working fine for particularly set data type (say for example for int instead of <T> it is working fine) but when I use generic then it doesn't work. It compiles error free but it doesn't even debug the area which is set to "toggle break point". Initially the GUI was like this: But as the control

Mapping Expression<Func<Type1,bool>> Expression<Func<Type2, bool>>

社会主义新天地 提交于 2019-12-07 23:00:20
问题 I want to pass my query from Business Layer to Service Layer but when doing this i have to convert my DTO to Entity model. Normally i can convert Type1 to Type2 via Autommaper but now i want to map Expression<Func<Type1,bool>> to Expression<Func<Type2, bool>> I got error from Automapper Missing type map configuration or unsupported mapping. Mapping types: Expression`1 -> Expression`1 How can i achieve this? 回答1: I just updated my answer to the another question you commented on, which I think

Use reflection to assign value of Func<Product, object> property

↘锁芯ラ 提交于 2019-12-07 16:43:48
问题 If I have a class Product: public class Product { public string Title { get; set; } public string Make { get; set; } public Decimal Price { get; set; } //(Edit) - Added non-string } And I have a property in another class declared as: Func<Product, object> SortBy { get; set; } I can set SortBy using: SortBy = p => p.Title; But how would I, using reflection, make the same assignment if I had the property name for SortBy stored as a string e.g. string sortField = "Title"; SortBy = /*Some

Dictionary<T, Func>: how to use T as Func's generic type?

≡放荡痞女 提交于 2019-12-07 15:57:33
问题 I didn't know how to express it clearly. I have this interface: interface IConverter { Dictionary<Type, Func<string, object>> ConversionMethods { get; } } Basically, it defines a contract saying that a class implementing it should provide conversion methods for all the custom types it uses (be them enums or anything). Is it possible to replace object in Func 's generic types with its corresponding dictionary key's type (so it is impossible to have two unmatching types)? I think it's not

Use a c# Func as part of an IQueryable without executing into memory call

蹲街弑〆低调 提交于 2019-12-07 11:25:32
问题 I'm trying to build a query that will execute against the database as an IQueryable, and not in memory (IEnumerable). The query will be used for several different purposes and each purpose has a slightly different way in which the Total property is calculated. Because I'm using a Func for calculating the total, i get an error advising me that sql doesn't know how to deal with the Invoke method of my Func, which is understandable. To get past the problem, i have had to list the groupings into

C# Fluent API With Dynamic Func<> Construction

穿精又带淫゛_ 提交于 2019-12-07 10:54:18
问题 I'm fooling around with creating a small SQL library with a fluent API and want to do something like this: var person = connection.GetOne<Person>("select * from [Person] where [Id] = 1") .WithMany<Pet>("select * from [Pet] where [PersonId] = 1") .WithMany<Address>("select * from [Address] where [PersonId] = 1]") .Build((person, pets, addresses) => { person.Pets = pets; person.Addresses = addresses; return person; }); I've built plenty of fluent API's before but all have been much more simple

Func variance with multiple parameters

半世苍凉 提交于 2019-12-07 08:10:34
问题 Tried to something like this in our code but it fails: Func<Employee, Employee> _myFunc; void Main() { Func<Employee, Employee> test1 = _myFunc;//Ok Func<Employee, Person> test2 = _myFunc;//Ok Func<Person, Employee> test3 = _myFunc;//Fails Func<Person, Person> test4 = _myFunc;//Fails } public class Person { } public class Employee : Person { } The last two cases give this error: Cannot implicitly convert type System.Func<Employee, Employee> to System.Func<Person, Employee> . An explicit

How to invoke Expression<Func<Entity, bool>> against a collection

邮差的信 提交于 2019-12-06 19:06:22
问题 I have an interface that defines a repository from the Repository pattern: interface IRepository { List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression); } I've implemented it against Entity Framework: class EntityFrameworkRepository { public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression) { return DBContext.Customers.Where(expression).ToList(); } } That seems to work well, it allows me to do something like: var customers =