Tag: func

使用ac #Func作为IQueryable的一部分,而不执行内存调用

我正在尝试构建一个将作为IQueryable而不是在内存(IEnumerable)中针对数据库执行的查询。 该查询将用于多个不同的目的,每个目的的计算Total属性的方式略有不同。 因为我使用Func来计算总数,所以我得到一个错误,告诉我sql不知道如何处理我的Func的Invoke方法,这是可以理解的。 为了解决这个问题,我不得不通过调用ToList()来将分组列入memor,这对性能不利。 有没有办法可以将此查询作为IQueryable执行? 否则我将不得不用计算方差将此查询写入20次以上 Func<IGrouping, double?> calculateTotal= (group) => @group.Sum(x => x.PassengerTotal); Dictionary weekValues = queryable.GroupBy(o => new { Year = SqlFunctions.DatePart(“yyyy”, o.DateCreated), Week = SqlFunctions.DatePart(“ww”, o.DateCreated), Source = o.SourceId, }) .ToList() //NEED TO REMOVE THIS CALL .Select(ac => new WeeklyGraphGroup() { Year = ac.Key.Year, Week = ac.Key.Week, SourceId = ac.Key.Source, Total = […]

反映MemberInfo到Func

我正在寻找一种方法将MemberInfo实例转换为“Func”类型(稍后通过lambda表达式使用它)。 让我们说我有一个类型的成员函数 public bool func(int); 使用reflection,我以某种方式得到MemberInfo “mi”的实例,现在我想将它转换为Func; 类型。 就像是: Func<int, bool f = myType.GetMember(mi.Name); 有办法吗? PS。 Marc Grawell的回答解决了我的问题,无需进一步评论

如何声明返回相同类型的Func委托的Func委托?

我想编写一个方法来做一些工作,最后返回另一个方法与原始方法相同的签名。 这个想法是依次处理一个字节流,而不是进入递归。 通过这样称呼它: MyDelegate executeMethod = handleFirstByte //What form should be MyDelegate? foreach (Byte myByte in Bytes) { executeMethod = executeMethod(myByte); //does stuff on byte and returns the method to handle the following byte } 要切换方法,我想将它们分配给Func委托。 但我遇到的问题是,这导致递归声明而没有终止… Func<byte, Func<byte, <Func>> 我不知何故在这里迷路了。 我怎么能绕过那个?

Func 的List ,使用generics返回类型编译错误,但为什么呢?

这是一个冗长的问题,所以请耐心等待。 我需要在一组字符串和每个字符串的相应generics方法调用之间创建一个映射。 但是,我遇到了编译问题,向下解释。 在我的场景中,我使用的是Dictionary ,但问题对于List同样存在。 为简单起见,我在下面的示例中使用List 。 考虑这三个类: public abstract class MyBase { /* body omitted */ } public class MyDerived1 : MyBase { /* body omitted */ } public class MyDerived2 : MyBase { /* body omitted */ } 还有一些其他类的方法: public class Test { public T GetT() where T : MyBase { /* body […]

通用表达式抽象问题

我有以下方法SetMapping() ,用于使用表达式定义一些映射设置。 public class AggregateMap { protected Expression<Func<IUpdateConfiguration, object>> graphMapping; protected void SetMapping(Expression<Func<IUpdateConfiguration, object>> mapping) { graphMapping = mapping; } } 调用代码示例: SetMapping(map => map.OwnedCollection(root => root.ChildEntities)); 上面的工作很好,但我想通过提供SetOwnedCollectionMapping()进一步抽象这个方法。 这意味着调用代码可以提供更基本的表达式。 进一步抽象的方法: protected void SetOwnedCollectionMapping(Expression<Func<TDataEntity, ICollection>> mapping) { graphMapping = map => map.OwnedCollection(mapping); } 调用代码示例: SetOwnedCollectionMapping(root => root.ChildEntities); 然后,通过在Entity Framework DbContext实例上调用以下方法,将此graphMapping字段用于外部库(RefactorThis.GraphDiff): public static void UpdateGraph(this DbContext context, […]

使用FluentValidation的WithMessage方法和命名参数列表

我正在使用FluentValidation,我想格式化一个对象的属性值的消息。 问题是我对C#中的表达式和委托的经验很少。 FluentValidation已经提供了一种使用格式参数执行此操作的方法。 RuleFor(x => x.Name).NotEmpty() .WithMessage(“The name {1} is not valid for Id {0}”, x => x.Id, x => x.Name); 我想做这样的事情,以避免在我更改参数的顺序时更改消息字符串。 RuleFor(x => x.Name).NotEmpty() .WithMessage(“The name {Name} is not valid for Id {Id}”, x => new { Id = x.Id, Name = x.Name }); 原始方法签名如下所示: public static IRuleBuilderOptions WithMessage( this IRuleBuilderOptions rule, string errorMessage, […]

连接两个Func委托

假设我有类: public class Order { int OrderId {get; set;} string CustomerName {get; set;} } 我也在下面声明变量 Func predicate1 = t=>t.OrderId == 5 ; Func predicate2 = t=>t.CustomerName == “Ali”; 有没有办法连接这些变量(用AND / OR)并将结果放在第三个变量? 例如: Func predicate3 = predicate1 and predicate2; 要么 Func predicate3 = predicate1 or predicate2;

简而言之,关于Func 可以说些什么

我现在一直在看Func ,我已经设法避开它(现在)。 但是,现在看起来我无法永远避开它。 例如,我尝试了Dynamic Linq,但几乎所有内容都是以Func 为例。 我已经尝试了我的一本书(C#2008 / Deitel&Deitel)以及MSDN,但我还没有得到它。 他们都直接跳入主题。 关于Func ,可以说(用几句话) 我可以在网上获得一些可以让我开始这个问题的链接吗? 谢谢你的帮助

从Func 到Func 的转换工作但到Func 失败

我有以下代码: static Func s_objToString = (x) => x.ToString(); static Func s_stringToString = s_objToString; //compiles static Func s_intToString = s_objToString; //error 第二行编译,但第三行无法编译错误: 无法将类型’ System.Func ‘隐式转换为’ System.Func ‘ 这是为什么? 我理解,虽然字符串是从对象派生的,但是List并不是从List派生的,但是这里object是string工作而object是int失败,为什么呢? 好吧,让我说我理解为什么; 现在的问题是有一种解决方法(其他然后定义MyInt类到框int因为Func到Func工作)?

如何创建具有表达式作为参数的通用表达式

ASP.Net MVC中有一个DisplayNameFor(x=>x.Title)帮助器。 我想在行为中实现类似的东西。 我想有一个方法接受基于User类( u=>u.Birthdate或u => u.Name)的表达式,一个操作数(Greater,Less,Equal)和一个像DateTime.Now这样的值并返回一个表达式u=>u.Birthdate > DateTime.Now 我知道我必须手工构建结果表达式。 我无法包裹的是传递和处理属性表达。 编辑: 我想调用一个类似的方法 GetFilterPredicate(u=>u.Birthdate,FilterOps.GreaterThan,DateTime.Parse(“01.01.2013”)或 GetFilterPredicate(u=>u.SomeIntProperty,FilterOps.Equals,2) 更新:我创建了一个包含此问题解决方案的repo以及一个集合属性过滤https://github.com/Alexander-Taran/Lambda-Magic-Filters