1

I am having two tables and I want data in such a manner that. I want to make this query in Linq. Can it be possible?

Table1

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

Table 2

pkTbl2Id       fkId    fkUserId
  1              1        2
  2              2        7

I want to fetch that records from table one which have

table1.SenderId == 2 || table1.ReceiverId == 2 && Table 2.fkId == Table1.pkId

the answer will be first records of First table i.e

1              2                5

I have used::

    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;
2
  • Please include code with what you have tried. Commented Dec 18, 2013 at 10:17
  • Can you please check if this is possible Commented Dec 18, 2013 at 10:26

1 Answer 1

1

If you want to count, you could use a join in both table to create this query and after it, count, for sample:

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();
Sign up to request clarification or add additional context in comments.

3 Comments

just one thing to add otherwise the above query is alright. Actually the situation is sender and reciever can be the same user. There can be the situation where receiver is there but sender is not and vice versa so in the above query I also want to add where t1.Receiverid=2 or t1.senderid=2
No problem, just add this condition in the where statement using OR operator ||. Look my edits!
sorry I am bothering you again but with reference to your query, my full query Is posted above which actually I want and also please check the tables, I have updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.