在 LINQ Query net core 3 中使用函数逻辑 [英] Using Function logic in LINQ Query net core 3

查看:30
本文介绍了在 LINQ Query net core 3 中使用函数逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下枚举:

      public enum WorkType
      {
        Type1,
        Type2,
        Type3,
        Type4,
        Type5,
        Type6
       }

和一个班级

      public class Work {
        public WorkType Type {get; set;}
        ....
      }

和一个扩展方法:

    public static partial class WorkTypeExtensions
    {
      public static bool IsHighValueWork(this WorkType value)
      {
        switch (value)
        {
            case WorkType.Type1:
            case WorkType.Type2:
                return true;

            default:
                return false;
        }
      }
    }

和 SQL Linq 查询

and SQL Linq query

  public List<Work> GetHighValueWork()
  {
    var query = Context.Work.Where( w => w.IsHighValueWork());
    return query.ToList();
   }

这是我问题的简化版本.这个查询曾经可以工作,但是在代码从 net core 2.1 转换到 3.1 之后它不再工作了.错误消息是无法翻译查询.要么以可翻译的形式重写查询,要么通过插入对 AsEnumerable() 或 AsAsyncEnumerable() 的调用显式切换到客户端评估.我不想把它改成

This is a simplified version of my problem. This query used to work, but it is not working any more after the code was converted from net core 2.1 to 3.1. The error msg is The query could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(). I don't want to change it to

 public List<Work> GetHighValueWork()
  {
    var query = Context.Work.Where( w => w.Type == WorkType.Type1 || w.Type == WorkType.Type2);
    return query.ToList();
   }

因为实际功能非常复杂.我搜索了一下,似乎可以使用 LINQ Expression Func,但我还没有想到.做这个的最好方式是什么?

Because actual function is very complex. I searched it seems LINQ Expression Func can be used, but I haven't figured that yet. What is the best way to do this?

推荐答案

IsHighValueWork 只是一个简单的 C# 方法.EF 无法将该函数转换为 SQL.

IsHighValueWork is just a simple C# method. There is no way to convert that function to SQL by EF.

确实很好地解释了 link,为什么它在 .net core 2.1 中工作.似乎在以前的版本中,当 EF Core 无法将作为查询一部分的表达式转换为 SQL 或参数时,它在客户端上自动评估了该表达式.强>

It is really explained well in that link, why it was working in .net core 2.1. It seems that, in previous versions when EF Core couldn't convert an expression that was part of a query to either SQL or a parameter, it automatically evaluated the expression on the client.

而且真的很糟糕.因为,如前所述:

And it is really bad. Because, as noted:

例如,Where() 调用中无法转换的条件可能导致表中的所有行都从数据库服务器,以及要应用于客户端的过滤器.

For example, a condition in a Where() call which can't be translated can cause all rows from the table to be transferred from the database server, and the filter to be applied on the client.

因此,以前您似乎只是将所有数据加载到客户端,然后在客户端应用过滤器.

So, it seems previously you were just loading all data to the client and then applying filter on the client side.

所以,你的代码的问题是,Func 不能被翻译成 Sql.要么将所有数据显式提取到应用程序中并进行过滤,要么使用您的代码的第二个版本.

So, the problem with your code is, that Func cant be translated into Sql. Either fetch all data into app explicitly and filter then or use second version of you code.

Context.Work.ToList()
       .Where( w => w.Type.IsHighValueWork());

但是,我不建议使用那个版本.最好像这样使用第二个版本:

But, I don't recommend to use that version. It is better to use second version like so:

Func<Work, bool> IsHighValueWork = (work) =>
            work.Type == WorkType.Type1 || work.Type == WorkType.Type2;

然后:

var query = Context.Work.Where(IsHighValueWork);

这篇关于在 LINQ Query net core 3 中使用函数逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆