检查LINQ中的另一个表中是否存在对象?

时间:2014-09-17 16:44:21

标签: c# asp.net sql-server linq entity-framework

我在实体框架中有一个查询,它从表中获取一些消息。

var ur = (from m in en.Messages
      join mb in en.aspnet_Membership on m.FromUserId equals mb.UserId
      join urs in en.UserProfiles on mb.UserId equals urs.UserId
      join g in en.Groups on m.ToUserId equals g.GroupId
      join ug in en.UserInGroups on g.GroupId equals ug.GroupId
      where ug.UserId == userId
      select new 
      {
          InboxId = m.MessageId,
          FromUser = urs.RaveName.Trim(),
          CreatedOn = m.CreatedOn
      }
      ).Concat(

      // msg is not deleted
      from m in en.Messages
      join mb in en.aspnet_Membership on m.FromUserId equals mb.UserId
      join urs in en.UserProfiles on mb.UserId equals urs.UserId
      where m.ToUserId == userId 
      select new 
      {
          InboxId = m.MessageId
          ,
          FromUser = urs.RaveName.Trim()          
          ,
          CreatedOn = m.CreatedOn
      }
      );

我有另一个表格,显示是否使用了该消息:

var msg = (from m in en.MessagesUsed
            where m.UserId == userId
            select m
            );

现在,我需要检查一下:'ur'中是否有一条消息不在'msg'中?在T-SQL中,我们可以使用:

SELECT 1
FROM ur 
WHERE NOT EXISTS (SELECT 1 FROM msg WHERE msg.Id = ur.InboxId AND msg.FromUser = ur.FromUser AND msg.CreatedOn = ur.CreatedOn)

检查一下。但是如何在LINQ中做到这一点?

由于

0 个答案:

没有答案