Search code examples
c#asp.netlinqentity-frameworkbooleanquery

ASP.NET MVC5 Entity Framework 6 get bool = true and bool = false LINQ


I have a table that I am filtering on. There is a filter for values 'include' which can be true or false.

I have a filter that has 3 options: true, false, & all.

So, when the filter is true, it should return rows where include = 'true'; when the filter is 'false', return where include = false; and when 'all' return where include = true or false.

Here is my code, that is not working, but I think it should be.

private ICollection<AggregationEntityViewModel> getEntities(AggregationPracticeDetailsViewModel apdvm)
{
    bool? filterInclude = Convert.ToBoolean(apdvm.Filter_IncludeValue);

    var a = (from e in _repository.GetAll<Entity>()                         
        where e.include == filterInclude != null ? (bool)filterInclude : (true || false)                     
        select e
     return a;
}

It is currently returning 0 rows when filter is set to 'All' or 'False', and returning all rows when set to 'Yes'.

FYI, I have ommitted lots of code for clarity's sake.

Please help...thanks!

*EDIT: I've displayed all the code, so you can see why I want to keep it all in linq query. Thanks for all the offered solutions. I see that most solutions involve using Linq Extension methods. Is there anyway to do it in inline linq query? *

bool? filterInclude = Convert.ToBoolean(apdvm.Filter_IncludeValue);
            var a = (from e in _repository.GetAll<Entity>()
                     from u in e.Users
                     where (e.AuditQuestionGroupId != null ? e.AuditQuestionGroupId : 0) == this.LoggedInEntity.AuditQuestionGroupId
                     && e.BatchNumber != null && e.BatchNumber.StartsWith(apdvm.Filter_BatchNumber == null ? "" : apdvm.Filter_BatchNumber)
                     && e.Name != null && e.Name.ToLower().StartsWith(apdvm.Filter_EntityName.ToLower())
                     && e.EntityState != null && e.EntityState.ToLower().Contains(apdvm.Filter_StateValue == null ? "" : apdvm.Filter_StateValue.ToLower())
                     && u.NIAMembershipId != null && u.NIAMembershipId.Contains(apdvm.Filter_MemberNo == null ? "" : apdvm.Filter_MemberNo)
                     from p in e.PracticeProfiles.DefaultIfEmpty()
                     join ea in _repository.GetAll<EntityAggregate>() on e.EntityId equals ea.EntityId into eas
                     from ea in eas.DefaultIfEmpty()                     
                     where ea.include == filterInclude != null ? (bool)filterInclude : (true || false)
                     group e by new { entity = e, profile = p, ea = ea } into newGroup
                     orderby newGroup.Key.entity.Name
                     select new AggregationEntityViewModel()
                     {
                         Id = newGroup.Key.ea == null ? 0 : newGroup.Key.ea.Id,
                         EntityId = newGroup.Key.entity.EntityId,
                         Include = newGroup.Key.ea == null ? (true || false) : (bool)newGroup.Key.ea.include,
                         BHAddress = newGroup.Key.profile == null || newGroup.Key.profile.soloOffice == null ? false : (bool)newGroup.Key.profile.soloOffice,
                         Incorporated = newGroup.Key.profile == null || newGroup.Key.profile.company == null ? false : (bool)newGroup.Key.profile.company,
                         MajorityOwned = newGroup.Key.profile == null || newGroup.Key.profile.capital == null ? false : (bool)newGroup.Key.profile.capital,
                         MajorityVoting = newGroup.Key.profile == null || newGroup.Key.profile.votingRights == null ? false : (bool)newGroup.Key.profile.votingRights,
                         Name = newGroup.Key.entity.Name,
                         Partnership = newGroup.Key.profile == null || newGroup.Key.profile.partnership == null ? false : (bool)newGroup.Key.profile.partnership,
                         PublicAccountant = newGroup.Key.profile == null || newGroup.Key.profile.publicAccountant == null ? false : (bool)newGroup.Key.profile.publicAccountant,
                         Trust = newGroup.Key.profile == null || newGroup.Key.profile.operatingTrust == null ? false : (bool)newGroup.Key.profile.operatingTrust,
                         TrustDeed = newGroup.Key.profile == null || newGroup.Key.profile.deed == null ? false : (bool)newGroup.Key.profile.deed
                     }).ToList();
            return a;

Solution

  • You could use

    private ICollection<AggregationEntityViewModel> getEntities(
                  AggregationPracticeDetailsViewModel apdvm)
    {
        bool? filterInclude = apdvm.Filter_IncludeValue.ConvertToNullable<bool>();
    
        var a = (from e in _repository.GetAll<Entity>()                         
                 where !filterInclude.HasValue || ea.include == filterInclude.Value                     
                 select new AggregationEntityViewModel()
                 {
                    Include = newGroup.Key.ea == null 
                              ? (true || false) 
                              : (bool)newGroup.Key.ea.include,
                 }
        return a;
    }
    

    just remove your (true||false) and add filterInclude == null in the where

    For Nullable Value (taken from Convert string to nullable type (int, double, etc...))

    public static T? ConvertToNullable<T>(this String s) where T : struct 
    {
        try
        {
            return (T?)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(s);
        }
        catch (Exception)
        {
            return null;
        }
    }