C# 在C中使用Linq获取选择性数据#

C# 在C中使用Linq获取选择性数据#,c#,asp.net,asp.net-mvc,linq,sql-server-2005,C#,Asp.net,Asp.net Mvc,Linq,Sql Server 2005,我有两个表,我需要这样的数据方式。我想在中进行此查询 林克。可能吗 Table1 pkId SenderId ReceiverId 1 2 5 2 2 2 3 2 7 4 3 2 我想从表一中取出那些有 table1.SenderId == 2 || table1.ReceiverId == 2 &a

我有两个表,我需要这样的数据方式。我想在中进行此查询 林克。可能吗

Table1

pkId    SenderId  ReceiverId
   1           2           5
   2           2           2
   3           2           7
   4           3           2

我想从表一中取出那些有

table1.SenderId == 2 || table1.ReceiverId == 2 && Table 2.fkId == Table1.pkId
答案是 第一个表的第一个记录 i、 e


我用过::

    return DB.table1
        .Where(x => 
            x.receiverId == 2 
            || x.senderID == 2 
            && DB.table2.Any(y => y.fkid == x.pkid))
        .Count();



 var ll = from rs in DB.tblMessages
                         join mm in DB.tblLogs
                             on rs.pkMessageId equals mm.fkMessageId
                         where rs.fkReceiverUserId == UserId || rs.fkSenderUserId == UserId && mm.fkUserID == UserId
                         select rs;

如果要计数,可以在两个表中使用一个
连接来创建此查询,然后使用count作为示例:

var query = from t1 in DB.table1
            join t2 in DB.table2 on t1.pkId equals t2.fkId
            where t1.ReceiverId == 2 || t1.SenderId == 2 
            select t1;

var countResult = query.Count();

请将您尝试过的代码包括在内。您能检查一下这是否可行吗?只需添加一件事,否则上面的查询就可以了。实际上情况是发送者和接收者可以是同一个用户。可能存在这样的情况,接收方在那里,而发送方不在,反之亦然,因此在上面的查询中,我还想添加where t1.Receiverid=2或t1.senderid=2没有问题,只需使用
运算符
|
where
语句中添加此条件即可。看我的编辑!很抱歉,我再次打扰您,但关于您的查询,我的完整查询已张贴在上面,实际上我需要,也请检查表格,我已更新。
    return DB.table1
        .Where(x => 
            x.receiverId == 2 
            || x.senderID == 2 
            && DB.table2.Any(y => y.fkid == x.pkid))
        .Count();



 var ll = from rs in DB.tblMessages
                         join mm in DB.tblLogs
                             on rs.pkMessageId equals mm.fkMessageId
                         where rs.fkReceiverUserId == UserId || rs.fkSenderUserId == UserId && mm.fkUserID == UserId
                         select rs;
var query = from t1 in DB.table1
            join t2 in DB.table2 on t1.pkId equals t2.fkId
            where t1.ReceiverId == 2 || t1.SenderId == 2 
            select t1;

var countResult = query.Count();