IQueryable,如何处理特殊情况
IQueryable, how to handle particular cases
我应该让 IQueryable 处理以下特殊情况:
s => s.Trusting == searchTrusting
s => s.Trusted == searchTrusted
s => s.Trusting == searchTrusting and s => s.Trusted == searchTrusted
other cases
我应该如何发出指定这些情况的 IQueryable 请求。
我以前从未使用过 IQueryable。
我的同事建议我使用:
var specialCases = IQueryable.
IQueryable.
他的评论是:
这种方法只能部分解决问题
尝试结合 IQueryable 和单独的 if 每种情况。
如何实现结合IQueryable?
在他发表评论之前ifs处于if,else if,else if,else的状态
为了获得相同的结果,我现有的解决方案使用了:(有效)
var domainTrustColl = _domainTrustRepository.GetDomainTrustItems().ToList().ConvertAll(new Converter<DomainTrustItem, DomainTrustModel>(DomainTrustModel.FromDomain));
domainTrustColl = domainTrustColl.Where(s => s.Trusting.Contains(searchTrusting)).Where(s => s.Trusted.Contains(searchTrusted)).Where(s => s.Type.Contains(searchType)).ToList();
if (!string.IsNullOrEmpty(searchTrusting))
{
domainTrustColl = domainTrustColl.Where(s => s.Trusting == searchTrusting).Where(s => s.Trusted.Contains(searchTrusted)).Where(s => s.Type.Contains(searchType)).ToList();
}
if (!string.IsNullOrEmpty(searchTrusted))
{
domainTrustColl = domainTrustColl.Where(s => s.Trusting.Contains(searchTrusting)).Where(s => s.Trusted == searchTrusted).Where(s => s.Type.Contains(searchType)).ToList();
}
if (!string.IsNullOrEmpty(searchTrusting) && !string.IsNullOrEmpty(searchTrusted))
{
domainTrustColl = domainTrustColl.Where(s => s.Trusting == searchTrusting).Where(s => s.Trusted == searchTrusted).Where(s => s.Type.Contains(searchType)).ToList();
}
从你的 LINQ 查询中,我发现这条语句是多余的
.Where(s => s.Type.Contains(searchType))
因此,您的 IQueryable
查询可以简化如下:
...
var query = domainTrustColl.Where(s => s.Type.Contains(searchType));
// Case - Both not selected
if (string.IsNullOrEmpty(searchTrusting) && string.IsNullOrEmpty(searchTrusted))
{
// Handle no select any case
}
// Third case - Both selected
else if (!string.IsNullOrEmpty(searchTrusting) && !string.IsNullOrEmpty(searchTrusted))
{
query = query.Where(s => s.Trusting == searchTrusting)
.Where(s => s.Trusted == searchTrusted);
}
// First case
else if (!string.IsNullOrEmpty(searchTrusting))
{
query = query.Where(s => s.Trusting == searchTrusting)
.Where(s => s.Trusted.Contains(searchTrusted));
}
// Second case
else if (!string.IsNullOrEmpty(searchTrusted))
{
query = query.Where(s => s.Trusting.Contains(searchTrusting))
.Where(s => s.Trusted == searchTrusted);
}
domainTrustColl = query.ToList();
最后,具体化查询以通过 .ToList()
执行查询。
我应该让 IQueryable 处理以下特殊情况:
s => s.Trusting == searchTrusting
s => s.Trusted == searchTrusted
s => s.Trusting == searchTrusting and s => s.Trusted == searchTrusted
other cases
我应该如何发出指定这些情况的 IQueryable 请求。 我以前从未使用过 IQueryable。
我的同事建议我使用:
var specialCases = IQueryable.
IQueryable.
他的评论是: 这种方法只能部分解决问题 尝试结合 IQueryable 和单独的 if 每种情况。
如何实现结合IQueryable?
在他发表评论之前ifs处于if,else if,else if,else的状态 为了获得相同的结果,我现有的解决方案使用了:(有效)
var domainTrustColl = _domainTrustRepository.GetDomainTrustItems().ToList().ConvertAll(new Converter<DomainTrustItem, DomainTrustModel>(DomainTrustModel.FromDomain));
domainTrustColl = domainTrustColl.Where(s => s.Trusting.Contains(searchTrusting)).Where(s => s.Trusted.Contains(searchTrusted)).Where(s => s.Type.Contains(searchType)).ToList();
if (!string.IsNullOrEmpty(searchTrusting))
{
domainTrustColl = domainTrustColl.Where(s => s.Trusting == searchTrusting).Where(s => s.Trusted.Contains(searchTrusted)).Where(s => s.Type.Contains(searchType)).ToList();
}
if (!string.IsNullOrEmpty(searchTrusted))
{
domainTrustColl = domainTrustColl.Where(s => s.Trusting.Contains(searchTrusting)).Where(s => s.Trusted == searchTrusted).Where(s => s.Type.Contains(searchType)).ToList();
}
if (!string.IsNullOrEmpty(searchTrusting) && !string.IsNullOrEmpty(searchTrusted))
{
domainTrustColl = domainTrustColl.Where(s => s.Trusting == searchTrusting).Where(s => s.Trusted == searchTrusted).Where(s => s.Type.Contains(searchType)).ToList();
}
从你的 LINQ 查询中,我发现这条语句是多余的
.Where(s => s.Type.Contains(searchType))
因此,您的 IQueryable
查询可以简化如下:
...
var query = domainTrustColl.Where(s => s.Type.Contains(searchType));
// Case - Both not selected
if (string.IsNullOrEmpty(searchTrusting) && string.IsNullOrEmpty(searchTrusted))
{
// Handle no select any case
}
// Third case - Both selected
else if (!string.IsNullOrEmpty(searchTrusting) && !string.IsNullOrEmpty(searchTrusted))
{
query = query.Where(s => s.Trusting == searchTrusting)
.Where(s => s.Trusted == searchTrusted);
}
// First case
else if (!string.IsNullOrEmpty(searchTrusting))
{
query = query.Where(s => s.Trusting == searchTrusting)
.Where(s => s.Trusted.Contains(searchTrusted));
}
// Second case
else if (!string.IsNullOrEmpty(searchTrusted))
{
query = query.Where(s => s.Trusting.Contains(searchTrusting))
.Where(s => s.Trusted == searchTrusted);
}
domainTrustColl = query.ToList();
最后,具体化查询以通过 .ToList()
执行查询。