none
Optional WHERE statement RRS feed

  • Question

  • Hi

    This is probably a very trivial question to answer....

    Imagine the following:

    private void PerformQuery(String account)
    {
    ....... (setting up the data context and the connection etc)

    var Users =
    from users in UserTable
    where users.accountName == account
    select new { ...};

    .... (rest of code)
    }

    Okay, it's possible that account == string.empty, in which case I really want to something similar to this:

    var Users =
    from users in UserTable
    if (account != string.empty)
    {
    where users.accountName == account
    }
    select new { ... }

    But this syntax is obviously not allowed....so, how can I optionally include/exclude a particular where statement?

    Thanks

    Griff
    Wednesday, September 10, 2008 1:57 PM

Answers

  • How about:


    var Users = 

    from users in UserTable

    where users.accountName == account && account != string.Empty

    select new { ...};


    [)amien

    Wednesday, September 10, 2008 3:00 PM
    Moderator
  • can't you just...

     

    private void PerformQuery(string account)

    {

    ... (other code)

     

    var Users = UserTable;

     

    if (!string.IsNullOrEmpty(account))

    Users = Users.Where(user => user.accountName == account);

     

    Users = Users.Select(u => new { ... });

     

    ... (rest of code)

    }

     

    ... or...

     

    private void PerformQuery(string account)

    {

    ... (other code)

     

    var Users = from user in UserTable select user;

     

    if (!string.IsNullOrEmpty(account))

    Users = from user in Users where user.accountName == account select user;

     

    Users = from user in Users select new { ... };

     

    ... (rest of code)

    }

    Wednesday, September 10, 2008 3:57 PM
  • You can add an optional Where clause by making a new query based on the old one.

     

    IQueryable<User> users = UserTable;

     

    if (acount != string.Empty)

    {

        users = from u in users

                    where u.accountName == account

                    select u;

    }

     

    var query = from u in Users

                    select new { ... };

     

     

    Thursday, September 11, 2008 3:55 AM
    Moderator

All replies

  • How about:


    var Users = 

    from users in UserTable

    where users.accountName == account && account != string.Empty

    select new { ...};


    [)amien

    Wednesday, September 10, 2008 3:00 PM
    Moderator
  • can't you just...

     

    private void PerformQuery(string account)

    {

    ... (other code)

     

    var Users = UserTable;

     

    if (!string.IsNullOrEmpty(account))

    Users = Users.Where(user => user.accountName == account);

     

    Users = Users.Select(u => new { ... });

     

    ... (rest of code)

    }

     

    ... or...

     

    private void PerformQuery(string account)

    {

    ... (other code)

     

    var Users = from user in UserTable select user;

     

    if (!string.IsNullOrEmpty(account))

    Users = from user in Users where user.accountName == account select user;

     

    Users = from user in Users select new { ... };

     

    ... (rest of code)

    }

    Wednesday, September 10, 2008 3:57 PM
  • You can add an optional Where clause by making a new query based on the old one.

     

    IQueryable<User> users = UserTable;

     

    if (acount != string.Empty)

    {

        users = from u in users

                    where u.accountName == account

                    select u;

    }

     

    var query = from u in Users

                    select new { ... };

     

     

    Thursday, September 11, 2008 3:55 AM
    Moderator