[英]Need help creating complex ef linq select
您好,我在創建具有多重聯接和左聯接的復雜Linq時遇到一些問題。
我最后列出了4個表格,這用於查看我需要如何發送有關主題的新回復的電子郵件。 因此,我從用戶加入的主題中獲取所有帖子。 同一用戶可能在每個主題中發表超過1條帖子。 之后,我與UserEmailSetting一起加入,以查看用戶是否選擇了接收電子郵件通知。 最后,我需要知道是否已經向用戶發送了一封電子郵件,通知新的回復(如果收到大量回復,我不想向用戶發送垃圾郵件),那么自上次發送回復通知以來,訪問該網站,我不想發送其他郵件。 這是我可行的嘗試,但我想對其進行優化! 問題是,UserEmailSetting上可能有很多結果,因此當我實際上只得到1或2時,我會得到很多OS結果。
這是我的專長
var select = (from p in ForumPostRepository.Get()
join u in UserRepository.Get() on p.UserId equals u.Id
join ues in UsersEmailSettingRepository.Get() on u.Id equals ues.UserId
join els in
(from _el in EmailLogRepository.Get()
where _el.Type == "ReplyToTopic" &&
_el.Values == topicId
orderby _el.Id descending
select _el) on u.Id equals els.UserId
into emailLogs
from el in emailLogs.DefaultIfEmpty()
where p.TopicId == forumTopic.Id &&
ues.ReplyToTopic //&& // We only want people who need notifications
//!u.Online // We only want people who are not online
orderby p.Id descending, el.Id descending
select new
{
User = u,
EmailLog = el
});
var result = select.DistinctBy(x => x.User.Id).ToList();
這是數據庫類
public class ForumPost
{
public int? TopicId { get; set; }
public int UserId { get; set; }
...
}
public class User
{
public int Id { get; set; }
public bool Online { get; set; }
public DateTime LastLogin { get; set; }
...
}
public class UsersEmailSetting
{
public int UserId { get; set; }
public bool ReplyToTopic { get; set; }
}
public class EmailLog
{
public int Id { get; set; }
public int UserId { get; set; }
public string Type { get; set; }
public string Values { get; set; }
public DateTime Created { get; set; }
}
Updata:我希望linq進行的結構化布局,希望對您有所幫助
不能保證這將是高性能的。
var users = UserRepository.Get();
var userEmailSettings = UsersEmailSettingRepository.Get();
var emailLogs = EmailLogRepository.Get();
var posts = ForumPostRepository.Get();
var foo =
from user in users
where posts
.Any(post => post.UserId == u.Id && post.TopicId == topicId)
where userEmailSettings
.Any(ues => u.Id equals ues.UserId && ues.ReplyToTopic == true)
where false ==
(
from el in emailLogs
where el.Type == "ReplyToTopic"
&& el.Values == topicId
&& el.UserId == user.Id
&& el.Created > user.LastLogin
select el
).Any()
select user;
簽
忍者忍者
編輯:只是看到您沒有導航屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.