[英]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.
对于值“ include”有一个过滤器,该过滤器可以为true或false。
I have a filter that has 3 options: true , false , & all . 我有一个具有3个选项的过滤器: true , false和all 。
So, when the filter is true, it should return rows where include = 'true'; 因此,当过滤器为true时,它应该返回其中include ='true';的行。 when the filter is 'false', return where include = false;
当过滤器为“ false”时,返回where include = false; and when 'all' return where include = true or false.
当“全部”返回时,其中include = true或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'. 当前,当filter设置为'All'或'False'时,它返回0行,而当设置为'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. *编辑:我已经显示了所有代码,因此您可以看到为什么我要将所有内容保留在linq查询中。 Thanks for all the offered solutions.
感谢所有提供的解决方案。 I see that most solutions involve using Linq Extension methods.
我看到大多数解决方案都涉及使用Linq Extension方法。 Is there anyway to do it in inline linq query?
无论如何,在嵌入式linq查询中可以做到这一点? *
*
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;
Convert.ToBoolean
returns bool
, not bool?
Convert.ToBoolean
返回bool
,不是bool?
, so there is no way filterInclude != null
is true
. ,所以没有办法
filterInclude != null
为true
。
You should use following pattern instead of ternary operator within where
clause: 您应该在
where
子句中使用以下模式,而不是三元运算符:
var query = _repository.GetAll<Entity>(); if (apdvm.Filter_IncludeValue == "true") query = query.Where(x => x.include == true); else if (apdvm.Filter_IncludeValue == "false") query = query.Where(x => x.include == false); return query;
I assumed apdvm.Filter_IncludeValue
is a string (and that's why you tried to call Convert.ToBoolean
on it). 我假设
apdvm.Filter_IncludeValue
是一个字符串(这就是您尝试在其上调用Convert.ToBoolean
的原因)。
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 只需删除您的
(true||false)
并在其中添加filterInclude == null
For Nullable Value (taken from Convert string to nullable type (int, double, etc...) ) 对于Nullable Value(取自将字符串转换为可为null的类型(int,double等) )
public static T? ConvertToNullable<T>(this String s) where T : struct
{
try
{
return (T?)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(s);
}
catch (Exception)
{
return null;
}
}
There is an other solution: 还有另一种解决方案:
var query = from e in _repository.GetAll<Entity>();
if (filterInclude.HasValue)
{
// when filterInclude is null (it means **ALL**),
// do not filter otherwise - check the flag
query = query.Where(entity => entity.Include == filterInclude.Value);
}
// or one-line:
// query = query.Where(entity => filterInclude == null
// || entity.Include == filterInclude.Value);
var a = query.Select(entity => new AggregationEntityViewModel { .... });
return a;
Other problem is that Convert.ToBoolean
never returns null
. 另一个问题是
Convert.ToBoolean
从不返回null
。 You should create own method to parse apdvm.Filter_IncludeValue
. 您应该创建自己的方法来解析
apdvm.Filter_IncludeValue
。
In order to convert to nullable type, you colud use the generic method: 为了转换为可为空的类型,您可以使用通用方法:
public static Nullable<T> ToNullable<T>(this string s) where T: struct
{
Nullable<T> result = new Nullable<T>();
try
{
if (!string.IsNullOrEmpty(s) && s.Trim().Length > 0)
{
TypeConverter conv = TypeDescriptor.GetConverter(typeof(T));
result = (T)conv.ConvertFrom(s);
}
}
catch { }
return result;
}
Usage: 用法:
var filterInclude = apdvm.Filter_IncludeValue.ToNullable<bool>();
You can make it easier with fluent syntax like this: 您可以使用流利的语法来简化此操作:
private ICollection<AggregationEntityViewModel> getEntities(AggregationPracticeDetailsViewModel apdvm)
{
var query = _repository.GetAll<Entity>();
if(apdvm.Filter_IncludeValue != 'all')
{
var value = Convert.ToBoolean(apdvm.Filter_IncludeValue);
query = query.Where(q => q.include == value)
}
return query.Select(q => new AggregationEntityViewModel {...}).ToArray();
}
no need to evaluate string to nullable bool or smth. 无需将字符串评估为可为null的布尔值或smth。 Same as no need to do strange boolean expressions.
与无需执行奇怪的布尔表达式相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.