方法X还没有支持转换为SQL还没有、转换为、方法、SQL

2023-09-04 11:55:19 作者:晗笑

我想写这应该得到一个用户对象和消息的用户已经发布量的查询。我这样做以下方式:

I want to write a query which should get an user object and the amount of messages the user has posted already. I did this the following way:

var query = (from u in _db.Repository<User>()
             where u.IsDeleted != true
             select new UserWithMessagecount()
             {
                 User = u
                 MessageCount = GetUserMessageCount(u.Documents).Count(),
             });

我使用的方法,因为一些消息应该被过滤掉(在一个动态的方式)

为了简单起见,我会发布功能,无需排序逻辑(这仍然产生相同的错误)。

To keep things simple I'll post the function without sorting logic (which still produces the same error).

    private EntitySet<Document> GetUserMessageCount(EntitySet<Document> set)
    {
        return set;
    }

返回的错误是:

The error returned is:

方法'X'没有支持转换为SQL。

Method 'x' has no supported translation to SQL.

在如何解决这个问题的任何想法?

Any ideas on how to fix this issue?

推荐答案

使用语法来代替:

 var query = (from u in _db.Repository<User>()
             let MessageCount = GetUserMessageCount(u.Documents).Count()
             where u.IsDeleted != true
             select new UserWithMessagecount()
             {
                 User = u,
                 MessageCount = MessageCount
             });