none
LINQ to SQL Conditions RRS feed

  • Question

  • HI!

    I want to make a LINQ to SQL request that handles two different conditions. One of them is listed in the function below. The condition I want to include in the function below is that I want to be able to request persons of any age. How do I rewrite this functions so it handles a request for 1. Persons of a certain age or 2: Persons of any age.

         public IQueryable test(int age)

            {

                var persons = from p in db.Person

                              where p.age = age

                              select new

                              {

                                  p.name

                              };

     

                return persons;

            }

    Monday, May 9, 2011 8:04 AM

Answers

  • HI!

    I want to make a LINQ to SQL request that handles two different conditions. One of them is listed in the function below. The condition I want to include in the function below is that I want to be able to request persons of any age. How do I rewrite this functions so it handles a request for 1. Persons of a certain age or 2: Persons of any age.

     

         public IQueryable test(int age)

            {

                var persons = from p in db.Person

                              where p.age = age

                              select new

                              {

                                  p.name

                              };

     

                return persons;

            }

     

     public IQueryable test(int age)

            {

                var persons = from p in db.Person

                              select  p.name;

                 if(age>0)  //if age<=0  ,select all persons
                {

                    persons =persons .Where(p=> p.age == age)
                }         

                return persons;

            }

     


    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, May 9, 2011 10:27 AM
  • Hi Won_Bin;

    Build the query in parts as shown in the code snippet. If the age value is greater then or equal to zero just filter on that one age. If age being passed is < 0 return all ages.

    public IQueryable test(int age)
    {
      var persons = db.Person.AsQueryable();
      
      if( age >= 0 )
      	persons = persons.Where( p => p.age == age );
      else
      	persons = persons.Where( p => p.age > 0 );
      	
      // Uncomment this line of code if you want a collection
      // of Person objects to be returned		
      // return persons.Select (p => p );
      
      // uncomment this line if you just want a collection 
      // of string of names to be returned 
      // return persons.Select (p => p.name );
    }
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, May 9, 2011 7:40 PM

All replies

  • HI!

    I want to make a LINQ to SQL request that handles two different conditions. One of them is listed in the function below. The condition I want to include in the function below is that I want to be able to request persons of any age. How do I rewrite this functions so it handles a request for 1. Persons of a certain age or 2: Persons of any age.

     

         public IQueryable test(int age)

            {

                var persons = from p in db.Person

                              where p.age = age

                              select new

                              {

                                  p.name

                              };

     

                return persons;

            }

     

     public IQueryable test(int age)

            {

                var persons = from p in db.Person

                              select  p.name;

                 if(age>0)  //if age<=0  ,select all persons
                {

                    persons =persons .Where(p=> p.age == age)
                }         

                return persons;

            }

     


    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, May 9, 2011 10:27 AM
  • Hi Won_Bin;

    Build the query in parts as shown in the code snippet. If the age value is greater then or equal to zero just filter on that one age. If age being passed is < 0 return all ages.

    public IQueryable test(int age)
    {
      var persons = db.Person.AsQueryable();
      
      if( age >= 0 )
      	persons = persons.Where( p => p.age == age );
      else
      	persons = persons.Where( p => p.age > 0 );
      	
      // Uncomment this line of code if you want a collection
      // of Person objects to be returned		
      // return persons.Select (p => p );
      
      // uncomment this line if you just want a collection 
      // of string of names to be returned 
      // return persons.Select (p => p.name );
    }
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, May 9, 2011 7:40 PM
  • Hi Won_Bin,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
     
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 12, 2011 1:54 AM
    Moderator