c# - 需要帮助创建复杂的 ef linq select

标签 c# entity-framework

您好,我在创建具有多重连接和左连接的复杂 Linq 时遇到了一些问题。

我最后列出了 4 个表,这是用来查看我需要如何发送有关对主题的新回复的电子邮件。所以我从用户加入的主题中获取所有帖子。同一个用户可能在每个主题中有超过 1 个帖子。 之后,我加入了 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 执行的操作更加结构化,希望对您有所帮助

  • 从 ForumPostRepository 中获取 topicId 为 13 的所有帖子
  • 在 ForumPostRepository.UserId = UserRepository.Id 上加入 UserRepository
  • 现在我只想要 unike 用户
  • 加入 UserRepository.Id 上的 UsersEmailSettingRepository = UsersEmailSettingRepository.UserId
  • 左加入 UserRepository.Id 上的 EmailLogRepository = EmailLogRepository.UserId 和 EmailLogRepository.Type = “ReplyToTopic” AND EmailLogRepository.Values = “topicId”
  • -> 现在这个请求可以有 0 到 * 个结果,我只想要最新的!

最佳答案

不保证这将是高性能的。

    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;

已签名

林克忍者

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

关于c# - 需要帮助创建复杂的 ef linq select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16387344/

相关文章:

asp.net - 集合已修改;枚举操作可能无法执行MVC

java - 如何针对这种特殊情况使用 Google App Engine 数据存储区设计实体结构?

java - 有 48 小时的差异,将 C# Ticks 转换为 Java 时间戳

c# - 用模板覆盖基类的抽象类

c# - 在不安装 Excel 的情况下运行 Excel 宏

c# - 如何修改 ListView 栏?

entity-framework - 我可以使用 Entity Framework (模型优先)来生成组合键吗?

c# - Entity Framework 6 中的多对多关系 + TPH 继承问题

c# - EF 4 - 没有代理的延迟加载

c# - 在新的 Azure SDK .NET 中创建快照并导出 - Azure.ResourceManager