Entities查询本地方法

Entities查询本地方法

本文介绍了我如何可以调用在LINQ to Entities查询本地方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

public void SalesCount(string customerId)
{
  ..
  ..
  return ...;
}

var resultQuery = dataContext.Customers
.Where (c => c.Name == "Alugili")
.Where (c => SalesCount(c.CustomerId) < 100);

当我执行resultQuery我得到一个转换为SQL例外。

When I execute resultQuery I get a translation to SQL exception.

我需要调用 SalesCount 其中,我能做到这一点是有没有办法解决这个问题!

I need to call SalesCount in the Where can I do that is there any workaround for this problem!

推荐答案

只要你不能。不能在SQL服务器上执行C#code直接,只能使用防爆pressions 和一些特殊功能的认可...

Simply you can't. You can't execute C# code on the SQL server directly, you can use only Expressions and some special recognized functions...

除非你改变你的查询(至少部分地)在LINQ到对象...

Unless you transform your query (at least partially) in a LINQ-to-Objects...

var resultQuery = dataContext.Customers
    .Where (c => c.Name == "Alugili")
    .AsEnumerable()
    .Where (c => SalesCount(c.CustomerId) < 100);

请注意,最后的其中,在客户端将被执行,那么多没用的行会从数据库中获取的。

Be aware that the last Where will be executed on the client side, so many useless rows will be fetched from the DB.

这篇关于我如何可以调用在LINQ to Entities查询本地方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:31