2017-09-04 25 views
0

查询GetAllForumMember,有什么建议吗?ABP框架LINQ to Entities for subquery'count'

ScreenShot

System.NotSupportedException:“LINQ实体无法识别 方法 'System.Linq.IQueryable`1 [Weixin.Kia.Community.ThreadComment] GETALL()' 方法,和这种方法不能被翻译成店 表达。”

+0

请添加代码而不是屏幕截图。有错误消息告诉你什么是问题。 LINQ to Entities无法将'GetAll'方法转换为SQL。尝试重构代码以使用数据库中的表而不是存储库。 – Scrobi

+0

感谢您更换代码的建议,而不是屏幕截图。 实际上,我不知道如何在EF中为count()构建子查询。 和错误消息是不能告诉我该怎么做的消息。 之类的。 select dth.ThreadMember tm select threadCount =(select(1)from dbo.Thread t WHERE t.userId = @ tm.userId)原始SQL实际上比EF简单得多。 –

+0

@Mcxie为什么使用'GetAll()'方法/扩展名,并且从哪个命名空间获得此方法/扩展名/程序集? – Progman

回答

0

在你的代码调用您的存储库类和Entity Framework无法翻译成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(); 

     } 
    } 
} 

请注意这是未经测试的代码。有一个使用实体框架的学习曲线& Linq-to-SQL,所以我建议你阅读教程。您还可以获得像LinqPad这样的工具,它可以帮助您习惯于在您的数据库中编写Linq查询。

我希望这有些帮助。如果不能随意更新您的问题并提供更多信息,例如包括您的模型或数据库上下文的代码。

+0

谢谢,我发现我们也可以使用导航属性。 –