Shawn Mclean
Shawn Mclean

Reputation: 57469

Filtering by 1 boolean value from 2 values from interface

Lets say I have a property that IsRegistrationCompleted.

I have 2 checkboxes in my filter interface that says:

  1. Complete
  2. Incomplete

If both are checked, then both complete and incomplete users are retrieved. If just Complete is checked, just completed users are retrieved. If none are checked, none are retrieved.

How do I put this logic in linq?

I binded my checkboxes as follows:

public bool IsCompleted { get; set; }
public bool IsInCompleted { get; set; }

My method is:

private void GetUsers()
{
        //TODO: Apply filters here.
        var query = _context.GetUsers(); //Returns IQueryable<User>

        _context.Load(query, LoadBehavior.MergeIntoCurrent, LoadApplicantsCompleted, null);
}

Using silverlight with WCF Ria Service.

Upvotes: 2

Views: 80

Answers (3)

Danexxtone
Danexxtone

Reputation: 773

I didn't test this yet but something like this might work:

(from user in users where IsCompleted && user.IsCompleted select user).Union(from user in users where IsInCompleted && !user.IsCompleted select user).Distinct()

Upvotes: 0

Piotr Auguscik
Piotr Auguscik

Reputation: 3681

In one query it will look like this:

Users.Where(r => (IsCompleted  && r.IsRegistrationCompleted) || (IsInCompleted && !r.IsRegistrationCompleted))

Upvotes: 1

Aducci
Aducci

Reputation: 26664

Something like this would work. I think it would be hard to read if you had all the following logic in a single Where statement

if(IsCompleted && IsInCompleted)
{
   query = query;
}
else if(IsCompleted)
{
  query = query.Where(u => u.IsRegistrationCompleted);
}
else if(IsInCompleted)
{
    query = query.Where(u => !u.IsRegistrationCompleted);
}
else
{
  query = query.Where(u => false);
}

Upvotes: 3

Related Questions