我如何获得选项< t>来自实体框架核心数据库调用?

发布于 2025-01-31 15:41:36 字数 1083 浏览 3 评论 0 原文

抱歉,如果这是一个愚蠢的问题,但是我正在与

在非功能代码中,我可以做类似的事情...

async Task DoFerretStuff(string id) {
  Ferret? ferret = await ctx.Ferrets.FirstOrDefaultAsync(f => f.id == id);
  if (ferret == null) {
    // Do whatever needs doing when we can't find the ferret
  } else {
    // Do ferret stuff
  }
}

我正在尝试以一种更有功能的方式执行此操作,并假设我能够做类似的事情...

async Task<Unit> DoFerretStuff(string id) =>
  new Option<Ferret>(await ctx.Ferrets.FirstOrDefaultAsync(f => f.Id == id))
    .Match(ferret => {
      // Do ferret stuff
      return unit;
    },
    () => {
      // Do whatever needs doing when we can't find the ferret
      return unit;
    });

但是,这给了编译器错误第一行...

不能从“雪貂”转换为'system.collections.generic.enumerable'

我不明白为什么,因为我认为您可以将(可能为null)对象传递到构造函数中的 option&lt; t&gt; t&gt; ,它将给出某些&lt; t&gt; none&lt; t&gt;

请有人可以解释我应该如何以功能性的方式做这种事情。

谢谢

Sorry if this is a dumb question, but I'm struggling with Language-Ext, and can't seem to find a neat way to do this.

In non-functional code, I could do something like this...

async Task DoFerretStuff(string id) {
  Ferret? ferret = await ctx.Ferrets.FirstOrDefaultAsync(f => f.id == id);
  if (ferret == null) {
    // Do whatever needs doing when we can't find the ferret
  } else {
    // Do ferret stuff
  }
}

I'm trying to do this in a more functional way, and assumed I would be able to do something like this...

async Task<Unit> DoFerretStuff(string id) =>
  new Option<Ferret>(await ctx.Ferrets.FirstOrDefaultAsync(f => f.Id == id))
    .Match(ferret => {
      // Do ferret stuff
      return unit;
    },
    () => {
      // Do whatever needs doing when we can't find the ferret
      return unit;
    });

However, this gives a compiler error on the first line...

cannot convert from 'Ferret' to 'System.Collections.Generic.IEnumerable'

I don't understand why, as I thought the idea was that you could pass a (possibly null) object into the constructor for Option<T> and it would give either a Some<T> or a None<T>.

Please can someone explain how I should do this sort of thing in a functional way.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2025-02-07 15:41:36

The Option constructor

尝试 pRELUDE.optional 函数而不是。这些过载中的一个具有无效的值,并将其转换为 option&lt; a&gt;

话虽如此,如果您想立即匹配之后执行另一个副作用,那么与/else 相比,您将获得什么?

FP通常涉及传递不变的值(例如 option&lt; ferret&gt; )到纯函数。该值可以来自不纯的操作,例如 ctx.ferrets.firstordefaultasync ,但它也可能只是当场创建的值。纯粹的功能不会在乎。

The Option constructor takes an IEnumerable<A> as argument, which explains the compiler error.

Try the static Prelude.Optional function instead. One of those overloads takes a nullable value and converts it to Option<A>.

All that said, if you want to immediately Match on it afterwards to perform another side effect, then what are you gaining, compared to if/else?

FP would typically involve passing an immutable value (such as Option<Ferret>) to a pure function. The value can come from an impure action, such as ctx.Ferrets.FirstOrDefaultAsync, but it could also just be a value created on the spot. A pure function wouldn't care.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文