我有以下方法返回一个IList<IGrouping<bool, int>>
,我想知道是否可以使用LINQ来做到这一点。(下面我写了测试用例,看看会发生什么,小数(<= 100)应该的布尔值是“是”,大数字(> 100)的布尔值应该是“否”。
public IList<IGrouping<bool, int>> GroupSmallAndBigNumbers(int[] numbers)
{
}
[Test]
public void NumbersSmallerThanOrEqualTo100AndBiggerNumbersCanBeGroupedUsingGroupBy()
{
//Arrange
int[] numbers = { 5, 700, 15, 108, 25, 28, 100 };
int[] expectedSmallNumbers = { 5, 15, 25, 28, 100};
int[] expectedBigNumbers = { 700, 108 };
//Act
var results = _examples.GroupSmallAndBigNumbers(numbers);
//Assert
Assert.That(results.Count, Is.EqualTo(2));
var smallNumbers = results[0];
Assert.That(smallNumbers, Is.EquivalentTo(expectedSmallNumbers));
var bigNumbers = results[1];
Assert.That(bigNumbers, Is.EquivalentTo(expectedBigNumbers));
}
答案 0 :(得分:3)
您可以通过检查每个数字是否大于100来进行分组。
public IList<IGrouping<bool, int>> GroupSmallAndBigNumbers(int[] numbers)
=> numbers.GroupBy(x => x >= 100).ToList();
答案 1 :(得分:0)
我认为Dictionary对您来说会更好(也更容易)。使用它,您可以轻松地执行以下操作:
❌ Environment aws://ACCOUNTIDHERE/us-west-2 failed bootstrapping: Error: Need to perform AWS calls for account ACCOUNTIDHERE, but no credentials found. Tried: default credentials.
at CredentialsCache.getCredentials (/usr/local/lib/node_modules/aws-cdk/lib/api/util/sdk.ts:261:11)
at CredentialsCache.get (/usr/local/lib/node_modules/aws-cdk/lib/api/util/sdk.ts:223:25)
at SDK.cloudFormation (/usr/local/lib/node_modules/aws-cdk/lib/api/util/sdk.ts:117:20)
at Object.deployStack (/usr/local/lib/node_modules/aws-cdk/lib/api/deploy-stack.ts:56:15)
at Object.bootstrapEnvironment (/usr/local/lib/node_modules/aws-cdk/lib/api/bootstrap-environment.ts:93:10)
at /usr/local/lib/node_modules/aws-cdk/bin/cdk.ts:270:24
at async Promise.all (index 1)
Need to perform AWS calls for account ACCOUNTIDHERE, but no credentials found. Tried: default credentials.