查詢GetAllForumMember
,有什麼建議嗎?ABP框架LINQ to Entities for subquery'count'
System.NotSupportedException:「LINQ實體無法識別 方法 'System.Linq.IQueryable`1 [Weixin.Kia.Community.ThreadComment] GETALL()' 方法,和這種方法不能被翻譯成店 表達。」
查詢GetAllForumMember
,有什麼建議嗎?ABP框架LINQ to Entities for subquery'count'
System.NotSupportedException:「LINQ實體無法識別 方法 'System.Linq.IQueryable`1 [Weixin.Kia.Community.ThreadComment] GETALL()' 方法,和這種方法不能被翻譯成店 表達。」
在你的代碼調用您的存儲庫類和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查詢。
我希望這有些幫助。如果不能隨意更新您的問題並提供更多信息,例如包括您的模型或數據庫上下文的代碼。
謝謝,我發現我們也可以使用導航屬性。 –
請添加代碼而不是屏幕截圖。有錯誤消息告訴你什麼是問題。 LINQ to Entities無法將'GetAll'方法轉換爲SQL。嘗試重構代碼以使用數據庫中的表而不是存儲庫。 – Scrobi
感謝您更換代碼的建議,而不是屏幕截圖。 實際上,我不知道如何在EF中爲count()構建子查詢。 和錯誤消息是不能告訴我該怎麼做的消息。 之類的。 select dth.ThreadMember tm select threadCount =(select(1)from dbo.Thread t WHERE t.userId = @ tm.userId)原始SQL實際上比EF簡單得多。 –
@Mcxie爲什麼使用'GetAll()'方法/擴展名,並且從哪個命名空間獲得此方法/擴展名/程序集? – Progman