How to incorporate a select statement in the where clause (linq to sql) RRS feed

  • Question

  • I have a person table that includes a person id and  a supervisor id for each employee

    To find a particular employees supervisor I would write a T-sql statement something like this

     Select p.username from person p where p.person_id = (select s.supervisor_Id from person s where s.username='jjohnson' )

    • How can I write this in linq to sql?



    Thursday, September 8, 2011 6:00 PM


  • Hi WTFHolmes;

    The following query should give you the needed results.

    var query = from p in Context.person
                where p.person_id == (from s in Context.person
                                      where s.username == "jjohnson"
                                      select s.supervisor_Id).FirstOrDefault()
                select p.username;

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Larcolais Gong Friday, September 16, 2011 2:55 AM
    Thursday, September 8, 2011 6:32 PM