在 foreach 循环中添加 LINQ Where && 条件
Adding LINQ Where && condition in a foreach loop
为了简化我的问题,假设我有以下数据库模型:
// database model, the list of not supported postcodes
var postcodeExclusionArray = new List<PostcodeExclusion>() {
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "A11"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "A12"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "A12 4FR"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "AR"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "B11"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "B12"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "C11"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "C12"
},
// many more records...
};
它描述了不受支持的英国邮政编码。它只是数据源的概念,因为我们通常使用 EF Core 3.1。请记住,每条记录的格式在收集整个区域时可能会有所不同,例如EH
或特定的 属性,例如AB25HZ
.
现在我想检查我的系统是否支持提供的英国邮政编码。
我的想法是仅使用 Where
语句在查询中执行此操作,但我不确定是否可行。
该算法的想法是以
等方式获得所提供邮政编码的组合
providedpostCode = "A41666";
组合:
A
A4
A41
A416
A4166
A41666
然后建立数据库查询如
bool isSupported = postcodeExclusionArray.Where(x => x.CustomerId == 2)
.Where(x=> x.Postocde == "A" ||
x.Postocde == "A" ||
x.Postocde == "A4" ||
x.Postocde == "A41" ||
x.Postocde == "A416" ||
x.Postocde == "A41666").First() == null;
有没有办法使用 LINQ(不是原始的 SQL)实现此目的,如果不是,最佳解决方案是什么?
我尝试使用 foreach
循环,但没有找到任何解决方案。
如果不可能,那么最简单的解决方案可能是生成原始 SQL 查询,
但是,这种方法的缺点是无法进行单元测试,只能通过集成测试进行测试。
另一种选择是通过 customerId 从数据库中查询所有邮政编码并进行简单的迭代。
有任何想法吗?
干杯
Enumerable.Contains
支持集合:
string providedpostCode = "A41666";
string[] providedPostCodes = Enumerable.Range(0, providedpostCode.Length)
.Select(ix => providedpostCode.Substring(0, ix + 1))
.ToArray();
bool isSupported = !postcodeExclusionArray
.Any(x => x.CustomerId == 2 && providedPostCodes.Contains(x.Postocde));
不确定逻辑,如果你需要Any
或!Any
。
看来您只需要 StartsWith
第一个字母:
var postCode = providedpostCode[0];
var isSupported = postcodeExclusionArray
.Where(x => x.CustomerId == 2)
.Where(x => x.Postocde.StartsWith(postCode));
无论如何,我认为这只是对问题的错误解释。
不想重复很多,只是重复使用来自这个的扩展可能会回答:
并编写以下代码(重复使用之前的答案代码):
var providedpostCode = "A41666";
var providedPostCodes = Enumerable.Range(0, providedpostCode.Length - 1)
.Select(ix => providedpostCode.Substring(0, ix + 1))
.ToArray();
var isSupported = postcodeExclusionArray
.Where(x => x.CustomerId == 2)
.FilterByItems(providedPostCodes, (x, c) => x.Postocde == c, true);
为了简化我的问题,假设我有以下数据库模型:
// database model, the list of not supported postcodes
var postcodeExclusionArray = new List<PostcodeExclusion>() {
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "A11"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "A12"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "A12 4FR"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "AR"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "B11"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "B12"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "C11"
},
new PostcodeExclusion(){
CustomerId = 2,
Postocde = "C12"
},
// many more records...
};
它描述了不受支持的英国邮政编码。它只是数据源的概念,因为我们通常使用 EF Core 3.1。请记住,每条记录的格式在收集整个区域时可能会有所不同,例如EH
或特定的 属性,例如AB25HZ
.
现在我想检查我的系统是否支持提供的英国邮政编码。
我的想法是仅使用 Where
语句在查询中执行此操作,但我不确定是否可行。
该算法的想法是以
等方式获得所提供邮政编码的组合providedpostCode = "A41666";
组合:
A
A4
A41
A416
A4166
A41666
然后建立数据库查询如
bool isSupported = postcodeExclusionArray.Where(x => x.CustomerId == 2)
.Where(x=> x.Postocde == "A" ||
x.Postocde == "A" ||
x.Postocde == "A4" ||
x.Postocde == "A41" ||
x.Postocde == "A416" ||
x.Postocde == "A41666").First() == null;
有没有办法使用 LINQ(不是原始的 SQL)实现此目的,如果不是,最佳解决方案是什么?
我尝试使用 foreach
循环,但没有找到任何解决方案。
如果不可能,那么最简单的解决方案可能是生成原始 SQL 查询,
但是,这种方法的缺点是无法进行单元测试,只能通过集成测试进行测试。
另一种选择是通过 customerId 从数据库中查询所有邮政编码并进行简单的迭代。
有任何想法吗?
干杯
Enumerable.Contains
支持集合:
string providedpostCode = "A41666";
string[] providedPostCodes = Enumerable.Range(0, providedpostCode.Length)
.Select(ix => providedpostCode.Substring(0, ix + 1))
.ToArray();
bool isSupported = !postcodeExclusionArray
.Any(x => x.CustomerId == 2 && providedPostCodes.Contains(x.Postocde));
不确定逻辑,如果你需要Any
或!Any
。
看来您只需要 StartsWith
第一个字母:
var postCode = providedpostCode[0];
var isSupported = postcodeExclusionArray
.Where(x => x.CustomerId == 2)
.Where(x => x.Postocde.StartsWith(postCode));
无论如何,我认为这只是对问题的错误解释。
不想重复很多,只是重复使用来自这个的扩展可能会回答:
并编写以下代码(重复使用之前的答案代码):
var providedpostCode = "A41666";
var providedPostCodes = Enumerable.Range(0, providedpostCode.Length - 1)
.Select(ix => providedpostCode.Substring(0, ix + 1))
.ToArray();
var isSupported = postcodeExclusionArray
.Where(x => x.CustomerId == 2)
.FilterByItems(providedPostCodes, (x, c) => x.Postocde == c, true);