locked
Lambda expression RRS feed

  • Question

  • HI

    How to convert this sql statement into Linq with lambda expression

    select idacuerdo

    from c1003

    where vigencia >= (select max(vigencia) from c1003 where vigencia <= '20130315')

    and vigencia <= '20130315'

    Thanks!

    Wednesday, March 20, 2013 9:02 AM

Answers

  • c1003.Where(c => c.vigencia >= c1003.Where(c1 => c1.vigencia <= 20130315).Max() && c.vigencia <= 20130315).Select(c => c.idacuerdo);


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    • Marked as answer by Mike Feng Thursday, April 4, 2013 9:07 AM
    Thursday, March 21, 2013 12:11 PM
  • Hi El.Conde;

    Something like the following query should give the same result as the SQL query.

    var filterDate = DateTime.Parse( "03/15/2013");
    
    var results = from c in dataContext.c1003
                  where c.vigencia >= (c1003.Where(cv => cv.vigencia <= filterDate).Max(cv => cv.vigencia))
                  select c.idacuerdo;

      


    Fernando (MCSD)

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

    • Marked as answer by Mike Feng Thursday, April 4, 2013 9:07 AM
    Thursday, March 21, 2013 3:02 PM

All replies

  •  

    Can you show the schema and data types for the table c1003?

      


    Fernando (MCSD)

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

    Wednesday, March 20, 2013 8:36 PM
  • Hi Fernando

    This is the schema and data types for the table c1003

    create table C1003 (
       IDACUERDO   int            identity,
       VIGENCIA      datetime   not null,
       COL1             int            not null,
       ...                  ...             ...
       COLN             int            not null,
       constraint PK_C1003 primary key (IDACUERDO)
    )

    can you help me ?

    Thanks

    Thursday, March 21, 2013 9:34 AM
  • c1003.Where(c => c.vigencia >= c1003.Where(c1 => c1.vigencia <= 20130315).Max() && c.vigencia <= 20130315).Select(c => c.idacuerdo);


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    • Marked as answer by Mike Feng Thursday, April 4, 2013 9:07 AM
    Thursday, March 21, 2013 12:11 PM
  • Hi El.Conde;

    Something like the following query should give the same result as the SQL query.

    var filterDate = DateTime.Parse( "03/15/2013");
    
    var results = from c in dataContext.c1003
                  where c.vigencia >= (c1003.Where(cv => cv.vigencia <= filterDate).Max(cv => cv.vigencia))
                  select c.idacuerdo;

      


    Fernando (MCSD)

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

    • Marked as answer by Mike Feng Thursday, April 4, 2013 9:07 AM
    Thursday, March 21, 2013 3:02 PM
  • Michael Samteladze, thank you very much !!
    Saturday, March 23, 2013 5:15 AM
  • Fernando Soto, thank you very much !! Indeed, speak Spanish ?
    Saturday, March 23, 2013 5:18 AM
  • Hi El.Conde;

    Has one of the two solutions given answered your question? If it has please go back to the post and check the "Mark As Answer" to complete the thread on the post that answered the question.

    Sí puedo hablar español mucho mejor de lo que se puede leer y escribir, así que me use Google Translate para traducir la pregunta y la respuesta posterior.

      


    Fernando (MCSD)

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

    Saturday, March 23, 2013 2:03 PM