子查询“计数”的ABP框架LINQ to Entities

时间:2017-09-04 10:03:31

标签: c# aspnetboilerplate

查询GetAllForumMember,有什么建议吗?

ScreenShot

  

System.NotSupportedException:“LINQ to Entities无法识别   方法'System.Linq.IQueryable`1 [Weixin.Kia.Community.ThreadComment]   GetAll()'方法,并且此方法无法转换为商店   表述“。

1 个答案:

答案 0 :(得分:0)

目前您的代码正在调用您的存储库类,而实体框架无法将其转换为SQL。

我已假设您的模型是如何构建的,但您可以使用数据库中的表而不是存储库来解决您的问题。

public class ForumMember
{
    public int Id { get; set; }
}

public class Member
{
    public int MemberId { get; set; }
}

public class ThreadComment
{
    public int CreatorUserId { get; set; }
}


public class ForumContext : DbContext
{
    public DbSet<ForumMember> ForumMembers { get; set; }
    public DbSet<Member> Members { get; set; }
    public DbSet<ThreadComment> ThreadComments { get; set; }
}

public class Repository
{
    public IEnumerable<Something> GetAllForumMember()
    {
        using (var context = new ForumContext())
        {
            var query = from fm in context.ForumMembers
                        join m in context.Members on fm.Id equals m.MemberId
                        //where add where clause
                        select new Something
                        {
                            memberId = m.MemberId,
                            commentCount = context.ThreadComments.Count(x => x.CreatorUserId == m.MemberId)
                            //... add other properties that you want to get.
                        };

             return query.ToList();

        }
    }
}

请注意,这是未经测试的代码。使用Entity Framework&amp; amp;有一个学习曲线。 Linq-to-SQL所以我建议你通过教程。您还可以获得LinqPad等工具,这可以帮助您习惯在数据库上编写Linq查询。

我希望这有点帮助。如果没有更多信息,请随时更新您的问题,例如:包括模型或数据库上下文的代码。