强制执行 IQueryable?

发布于 2024-07-18 05:44:57 字数 85 浏览 4 评论 0原文

我有一个“没有转换为 SQL”的方法,我想在 IQueryable 上执行该方法,有没有办法强制 IQueryable 执行,而不必将其存储在某个中间类中?

I have a method that 'has no translation to SQL' that I want to perform on an IQueryable, is there a way to force the IQueryable to execute without having to store it in some intermediate class?

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

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

发布评论

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

评论(3

带刺的爱情 2024-07-25 05:44:57

问题是您希望您的方法在本地执行而不是在数据库中执行吗? 如果是这样,AsEnumerable 就是你的朋友。 这是一个非常简单的方法,类似于:

public IEnumerable<T> AsEnumerable(IEnumerable<T> source)
{
    return source;
}

重要的是它使结果的编译时类型为 IEnumerable 而不是 IQueryable,这意味着您之后调用的任何 LINQ 查询运算符都将是 LINQ to Objects 而不是 LINQ to SQL。

例如:

var query = context.Employees
                   // Filtering performed in SQL
                   .Where(emp => emp.IsFullTime)
                   .AsEnumerable()
                   // Projection performed locally; ComputeSalary has no
                   // SQL equivalent
                   .Select(emp => new { Employee = emp,
                                        Salary = ComputeSalary(emp) });

您可以按照其他地方的建议调用 ToList,但如果您正在进行过滤并且实际上不需要内存中的完整列表,请调用 AsEnumerable< /code> 并过滤结果将比首先加载所有内容更有效。

Is the problem that you want your method to execute locally rather than in the database? If so, AsEnumerable is your friend. It's a very simple method, something like:

public IEnumerable<T> AsEnumerable(IEnumerable<T> source)
{
    return source;
}

The important thing is that it makes the compile-time type of the result IEnumerable<T> rather than IQueryable<T>, which means any LINQ query operators you call after that will be the LINQ to Objects ones instead of LINQ to SQL.

For example:

var query = context.Employees
                   // Filtering performed in SQL
                   .Where(emp => emp.IsFullTime)
                   .AsEnumerable()
                   // Projection performed locally; ComputeSalary has no
                   // SQL equivalent
                   .Select(emp => new { Employee = emp,
                                        Salary = ComputeSalary(emp) });

You could call ToList as suggested elsewhere, but if you're doing filtering and don't really need the full list in memory, calling AsEnumerable and filtering that result will be more efficient than loading everything first.

我为君王 2024-07-25 05:44:57
List<Employees> myEmployees =  myqueryable.ToList();

然后你就可以在这个列表上做你的 linq 事情了。

List<Employees> myEmployees =  myqueryable.ToList();

and then you can do your linq stuff on that List.

許願樹丅啲祈禱 2024-07-25 05:44:57

当您编写了一个 LinqToSql 不知道如何转换为 SQL 的查询(这也是它所说的)时,您会收到该消息。

我不确定我是否完全明白您的要求,但据我所知,您有以下选择:

  1. 重写您的查询,以便 LinqToSql 可以翻译它
  2. 在 Sql Server 上执行尽可能多的查询,然后在内存中完成其余的操作(使用 linq 到对象)
  3. 坐下来哭泣

假设我们排除了#3,让我们看看其他 2 个示例。

  1. 重写它 - 为了帮助解决这个问题,我们需要您的 linq 查询。

  2. 在这里,您从初始查询中取出无法翻译的部分,然后在 Iqueryable 上调用 ToList,然后在该列表上应用查询的其余部分。

您是否可以执行查询而不存储它? 好吧,不是真的,您总是可以循环遍历结果,因此不将其存储在变量中,但显然查询的结果需要存储在某个地方。

You get that message when you have written a query that LinqToSql doesn't know how to translate into SQL (which is what it says too).

I am not sure I get exactly what you're asking, but as far as I see, you have the following options:

  1. Rewrite your query so that LinqToSql CAN translate it
  2. Do as much of the query as you can on the Sql Server, then do the rest in memory (using linq to objects)
  3. Sit down and cry

Assuming we rule out #3, let's look at the other 2 examples.

  1. Rewriting it - to help with that, we need your linq query.

  2. Here you take out the part that can't be translated from the initial query, then on your Iqueryable call ToList, and then apply the rest of the query on that list.

And can you execute the query without having to store it? Well, not really, you could always loop through the results and as such not store it in a variable, but obviously the results of the query needs to be stored somewhere.

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