需要帮助创建复杂的ef linq选择

本文关键字:ef linq 选择 复杂 帮助 创建 | 更新日期: 2023-09-27 18:12:17

你好,我有一些问题,创建一个复杂的Linq与多连接和左连接。

我有4个表在最后列出,这是用来看看我需要如何发送关于一个主题的新回复的电子邮件。所以我从用户加入的主题中获取所有帖子。同一个用户可以在每个主题中有多个帖子。之后,我加入UserEmailSetting,这是为了看看用户是否已经停止接收电子邮件通知。最后,我需要知道是否已经向用户发送了关于新回复的电子邮件(如果有很多回复,我不想向我的用户发送垃圾邮件),所以如果自上次访问网站以来已经发送了回复通知,我不想再发送另一封邮件。这是我的尝试,工作,但我想优化它!问题是,在UserEmailSetting上可以有很多结果,所以当我实际上只得到1或2个结果时,我得到了很多结果。

这是我的尝试

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做的更结构化的布局,我希望它有帮助

  • 获取forumpostrerepository中topicId为13的所有帖子
  • 在ForumPostRepository上加入UserRepository。UserId = UserRepository。Id
  • 现在我只想要不同的用户
  • 与UserRepository上的UsersEmailSettingRepository连接。Id =UsersEmailSettingRepository。UserId
  • 与UserRepository上的EmailLogRepository左连接。Id =EmailLogRepository。UserId和emaillogrerepository。类型="ReplyToTopic"和emaillorepository。Values = " topicId "
  • ->现在在这个请求上可以有从0到*的任何结果,我只想要最新的!

需要帮助创建复杂的ef 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;
签署的

A Linq Ninja

编辑:刚刚看到你没有导航属性