none
Subuery in where clause in LINQ

    Question

  • Hi.

    I'm new in linq, and I'm trying to do the following query to linq

    select id, nombre

    from trabajadores

    where idjefe = (select codigo from trabajadores where id= 1233)

    Can somebody help me!

    • Moved by Naomi N Wednesday, April 30, 2014 1:03 AM Better answer can be here
    Wednesday, April 30, 2014 12:15 AM

Answers

  • Hi jbarrioss;

    Try something like the following query.

    var results = from t in YourDBContext.trabajadores
                  where t.idjefe == YourDBContext.trabajadores.Where( t2 => t2.id == 1233 ).Select( t2 => t2.codigo ).FirstOrDefault()
                  select new
                  {
                      ID = t.id,
                      Nombre = t.nombre
                  }; 
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by jbarrioss Monday, May 12, 2014 3:54 PM
    Monday, May 05, 2014 4:34 PM

All replies

  • Hello,

    For a subquery in linq, in your case, you can follow this sample:

    var result = (from OA in db.Orders
    
                                  where OA.OrderName == db.Orders.Where(order => order.OrderID == 1).FirstOrDefault().OrderName
    
                                  select OA).ToList();
    

    For more information regarding this topic, you can refer to below link:

    http://stackoverflow.com/questions/418609/how-to-do-subquery-in-linq

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 02, 2014 1:44 AM
  • Hi jbarrioss;

    Try something like the following query.

    var results = from t in YourDBContext.trabajadores
                  where t.idjefe == YourDBContext.trabajadores.Where( t2 => t2.id == 1233 ).Select( t2 => t2.codigo ).FirstOrDefault()
                  select new
                  {
                      ID = t.id,
                      Nombre = t.nombre
                  }; 
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by jbarrioss Monday, May 12, 2014 3:54 PM
    Monday, May 05, 2014 4:34 PM
  •   

    Is this still an open issue or has one of the solutions posted on this thread answer the question?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Saturday, May 10, 2014 12:56 PM