Reputation: 57469
Lets say I have a property that IsRegistrationCompleted
.
I have 2 checkboxes in my filter interface that says:
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
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
Reputation: 3681
In one query it will look like this:
Users.Where(r => (IsCompleted && r.IsRegistrationCompleted) || (IsInCompleted && !r.IsRegistrationCompleted))
Upvotes: 1
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