locked
"AND" in linq? is it possible? RRS feed

  • Question

  • Hello people, I've got very simple question. Is it possible in linq just like in SQL language use "and"? Just like select:

    select name, surname from person where name = 'John' and datebirth > 01/01/1990;

    So I want to use the same in c# linq:

    var data = a from db.gettable<person>
                   where a.name == 'John'
                   ?and? a.datebirth > 01/01/1990
                   select a.name?,? a. surname;

    ...another question within this...is it possible to use ',' within select in c# linq?
    Tuesday, October 21, 2008 7:16 AM

Answers

  • Hi

    For using 'and' you should use '&&'  Like this

    var data = a from db.gettable<person>
                   where a.name == 'John'
                   && a.datebirth > 01/01/1990
                   select a.name?,? a. surname;


    and for projecting data you can use anonymous type with 'New Operator' Like this

    var data = a from db.gettable<person>
                   where a.name == 'John'
                   && a.datebirth > 01/01/1990
                   select new {Name =
    a.name ,  Surname = a.surname};
    Tuesday, October 21, 2008 7:35 AM
  •  Mato_Slovakia wrote:

    ...but still the question about "," remains...

     

    select new { a.name, a. surname } ;

    Tuesday, October 21, 2008 7:25 AM
  • Hi,

     

    you can do it like this:

     

     

    var data = a from db.gettable<person>
                   where a.name.Equals("John")
                   &&  a.datebirth > 01/01/1990
                   select  new

    {

         name = a.name,

         surname = a.surname

    };

     

    Regards

    Tuesday, October 21, 2008 7:49 AM

All replies

  • Aha, of course "and" is "&&", I found in the meantime here:

    http://msdn.microsoft.com/en-us/vcsharp/aa336760.aspx#WhereSimple2

    ...but still the question about "," remains...
    Tuesday, October 21, 2008 7:20 AM
  •  Mato_Slovakia wrote:

    ...but still the question about "," remains...

     

    select new { a.name, a. surname } ;

    Tuesday, October 21, 2008 7:25 AM
  • Hi

    For using 'and' you should use '&&'  Like this

    var data = a from db.gettable<person>
                   where a.name == 'John'
                   && a.datebirth > 01/01/1990
                   select a.name?,? a. surname;


    and for projecting data you can use anonymous type with 'New Operator' Like this

    var data = a from db.gettable<person>
                   where a.name == 'John'
                   && a.datebirth > 01/01/1990
                   select new {Name =
    a.name ,  Surname = a.surname};
    Tuesday, October 21, 2008 7:35 AM
  • Hi,

     

    you can do it like this:

     

     

    var data = a from db.gettable<person>
                   where a.name.Equals("John")
                   &&  a.datebirth > 01/01/1990
                   select  new

    {

         name = a.name,

         surname = a.surname

    };

     

    Regards

    Tuesday, October 21, 2008 7:49 AM
  • Thank you for very nice answer Smile
    Tuesday, October 21, 2008 9:13 AM