热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

表达>fromaf#Func。-Expression>fromaF#func

inlinq,.WheretakesaExpression>predicate,whichIcanwriteinF#as在linq中,在哪里取一个表达式>谓词

in linq, .Where takes a Expression> predicate, which I can write in F# as

在linq中,在哪里取一个表达式>谓词,我可以在f#中写入。

<@ fun item:'a -> condition @>    // Expr<'a -> bool>

I'm using FSharp.Powerpack to build the expression from a quotation, but what it gives me is a MethodCallExpression. Looking deep, the powerpack code builds the lambda correctly, but wraps it in a Convert call (why is that?). I wonder if casting the argument to the method call (a lambda) would finally give me the Expression> I need.

我用FSharp。Powerpack通过引用来构建表达式,但是它给我的是一个MethodCallExpression。看起来很深奥,powerpack代码正确地构建了lambda,但是将它封装在一个转换调用中(为什么呢?)我想知道是否将这个参数转换为方法调用(一个lambda)最终会给出我需要的>表达式。

So the question is why the Convert call, and how to actually get the lambda with the Func signature.

问题是为什么转换调用,以及如何使用Func签名来得到lambda。

2 个解决方案

#2


5  

One way you can now do this is to take advantage of the fact that F# will perform this conversion automatically when invoking methods on .NET types that expect a Expression>.

现在可以这样做的一种方法是利用f#将在调用. net类型的方法时自动执行此转换,而这种方法期望表达式 >。

I'm not entirely sure when this got added to the language, but certainly with F# 4, you don't need to explicitly convert F# expressions into LINQ ones. If the reason you wanted to do this in the first place was to be able to use IQueryable LINQ APIs (or other expression-based .NET APIs) then it now just works with no effort, e.g.:

我不完全确定这是什么时候添加到语言中,但是在f# 4中,您不需要显式地将f#表达式转换为LINQ。如果您想要这样做的原因是能够使用IQueryable LINQ api(或其他基于expressnet的. net api),那么现在它就可以不费力地工作了,例如:

someEfDataContext.MyEntities.Single(fun e -> e.Id = 42)

just works. Even though this looks like an ordinary lambda (we've not used F#'s expression syntax), this compiles to code that produces an F# expression object, and then passes that to LeafExpressionConverter‌​.QuotationToExpressi‌on to turn it into a LINQ expression object.

只是工作。即使这看起来像一个普通的λ(我们没有使用f#表达式语法),这种编译代码,产生一个f#表达式对象,然后将LeafExpressionConverter‌。QuotationToExpressi‌,把它变成一个LINQ表达式对象。

But sometimes you'll want to get hold of the LINQ-style expression object directly in F#. (E.g., sometimes it's useful to write an F# function that produces an expression that you'll use in multiple queries.) In that case you can write a helper like this:

但有时您会希望直接在f#中获得linqstyle表达式对象。(例如,有时编写一个f#函数来生成一个在多个查询中使用的表达式是很有用的。)在这种情况下,你可以这样写一个帮手:

type FunAs() =
    static member LinqExpression<'T, 'TResult>(e: Expression>) = e

This looks like it does nothing - it just returns its argument. However, because FunAs is a .NET type, F# will automatically compile any call site that invokes this with a fun expression into code that generates a suitable LINQ query expression. E.g.:

看起来它什么都不做——它只是返回它的参数。但是,因为FunAs是一个。net类型,f#将自动编译任何调用站点,它将一个有趣的表达式转换为代码,生成合适的LINQ查询表达式。例如:

let linqExpr = FunAs.LinqExpression(fun (e:MyEntity) -> e.Id = 42)

Here, linqExpr will be of type Expression>.

这里,linqExpr将是类型表达式 >。

The key to this is that this method is a member of a .NET Type. If you try the exact same thing with an ordinary F# function:

关键是这个方法是。net类型的成员。如果你用一个普通的f#函数来做同样的事情:

let funAsLinqExpression<'T, 'TResult>(e: Expression>) = e

which seems like it should mean exactly the same thing as FunAs.LinqExpression, you'll find that you can't call it in the same way. E.g., if you try this:

这看起来应该和FunAs完全一样。LinqExpression,你会发现你不能用同样的方法调用它。如果你尝试一下:

let linqExpr = funAsLinqExpression(fun (e:MyEntity) -> e.Id = 42)

You'll get a (slightly unhelpful) error: 'This function takes too many arguments, or is used in a context where a function is not expected`.

您将会得到一个(稍微没有帮助的)错误:“这个函数使用了太多的参数,或者是在不需要函数的情况下使用”。

By making this function a member of a .NET type, we can take advantage of F#'s helpful "You seem to be invoking a .NET API that expects a LINQ-style expression, let me take care of that for you" feature.

通过使这个函数成为。net类型的成员,我们可以利用f#的帮助“您似乎正在调用一个期望linq风格表达式的. net API,让我来为您处理”特性。

(It's possible that there's some more explicit way of asking the LINQ compiler to perform this same trick for you without bringing a .NET type into the picture, but I've not found it.)

(可能有一些更明确的方式要求LINQ编译器为您执行相同的技巧,而无需将. net类型引入到图片中,但我没有找到它。)


推荐阅读
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 互联网世界 9 种基本的商业模式
    互联网世界9种基本的商业模式一个商业模式是运行一个公司的方法;通过该模式的运作,一个公司能维持自己的生存,就是说,能有收益。商业模式意味着一个公司是如何通过在价值链中定位自己,从而获 ... [详细]
  • 本文分析HashMap的实现原理。数据结构(散列表)HashMap是一个散列表(也叫哈希表),用来存储键值对( ... [详细]
  • 最近学习了数据挖掘常用的两种算法:FP-Growth和K-Means。现在把我的学习结果分享给大家。以下是本文的目录,大家可以根据需要跳过一些章节:1.FP-Grow ... [详细]
  • Android开发之Notification(实现消息弹窗、提示音以及点击事件)
    文章目录通知管理器通知渠道通知发送通知更多效果添加点击事件取消消息通知管理器通知管理器(NotificationManager)类是一个通知管理器&# ... [详细]
  • 数据的输出与数值传递
    1.数据的基本输出通过关键字WRITE的声明,能够实现数据对象的输出,并能够实现输出对象的长度、输出位置及格式等属性的控制。与变量定义一样,WRITE可以同时输出多个数据对象,对于 ... [详细]
  • 883.三维形体投影面积
    题目883.三维形体投影面积题目大意在nxn的网格grid中,我们放置了一些与x,y,z三轴对齐的1x1x1立方体。每个值vgri ... [详细]
  • The“travellingsalesmanproblem”asksthefollowingquestion:“Givenalistofcitiesandthedistancesb ... [详细]
  • 1011-MarriageCeremoniesPDF(English)StatisticsForumTimeLimit:2second(s)MemoryLimit:32MBYouw ... [详细]
  • 726:ROADS726:ROADS总时间限制:1000ms内存限制:65536kB描述Ncitiesnamedwithnumbers1Nareconnectedwithon ... [详细]
  • 【JVM技术专题】深入分析CG管理和原理查缺补漏「番外篇」
    前提概要本文主要针对HotspotVM中“CMSParNew”组合的一些使用场景进行总结。自Sun发布Java语言以来,开始使用GC技术来进行内存自动管理࿰ ... [详细]
  • Maven 无法打包jar到 Tomcat
    今天新来的同事,让他调试短信验证的接口。跟我说添加的aliyun的短信验证服务无法在tomcat上运行。然后给我看了报错信息:java.lang.NoClassDefFoundEr ... [详细]
  • 内容多有疏漏,有问题欢迎提出目录java内存模型的概念原子性(Atomicity)可见性(Visibility࿰ ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
author-avatar
一个具有骨感的女人0
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有