Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c# - Sql Query with not in clause of subquery to LINQ query

   select * from user_user_connection 
where userid_from = 3464 and 
userid_to not in
(select userid_from from 
user_user_connection 
where userid_to = 3464);

Hi helpies I am new to entity framework. I am trying to convert this query into LINQ query not able to understand how can I write the subquery with not in clause. Also which one should be better join or Include?

question from:https://stackoverflow.com/questions/65890430/sql-query-with-not-in-clause-of-subquery-to-linq-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This

    from a in user_user_connection 
    join b in user_user_connection on 
        new {From=a.userid_to, To=a.userid_from} equals new {From=b.userid_from, To=b.userid_to} into c
    from d in c.DefaultIfEmpty()
    where d == null
    select a;

is similar to

select a.*
from user_user_connection a
left join user_user_connection b on a.userid_to = b.userid_from and a.userid_from = b.userid_to
where b.userid_from is null

which should match your not in query.

if you want a specific userid_from you can add another where

    from a in user_user_connection 
    where a.userid_from == 3464
    join b in user_user_connection on 
        new {From=a.userid_to, To=a.userid_from} equals new {From=b.userid_from, To=b.userid_to} into c
    from d in c.DefaultIfEmpty()
    where d == null
    select a;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...