I need a linq expression that will find the nearest numbers (both "greater or equal" and "less or equal") in an array for a given number.
我需要一个linq表达式,它将在给定数字的数组中找到最接近的数字(“大于或等于”和“小于或等于”)。
E.g. Array - 1, 33, 66, 100
例如。阵列 - 1,33,66,100
If I have the number 10, I want to return 1 and 33. If I have the number 70, I want to return 66 and 100. If I have the number 33, I want to return 33 and 66.
如果我有数字10,我想返回1和33.如果我有数字70,我想返回66和100.如果我有数字33,我想返回33和66。
I could do this with some kind of basic for loop, but this is an operation on numbers in a database, so I'd prefer a linq to sql expression.
我可以用某种基本的for循环来做这个,但这是对数据库中的数字的操作,所以我更喜欢linq到sql表达式。
Edit: I was actually searching for a single linq expression to achieve this, but perhaps I was being a little hopeful :)
编辑:我实际上是在搜索单个linq表达式来实现这一点,但也许我有点希望:)
3 个解决方案
#1
Similar to Andrews answer, but I prefer doing the filter before the OrderBy, which reduces the amount of data the query has to run through. Also OrderBy.First is the same as Min, and OrderByDescending.First is the same as Max.
与Andrews的答案类似,但我更喜欢在OrderBy之前进行过滤,这样可以减少查询必须运行的数据量。 OrderBy.First也与Min相同,OrderByDescending.First与Max相同。
var high = list
.Where(i => i > n)
.DefaultIfEmpty()
.Min();
var low = list
.Where(i => i <= n)
.DefaultIfEmpty()
.Max();
#2
var list = new[] { 1, 33, 66, 100 };
var n = 33;
var high = list
.OrderBy(i=>i)
.FirstOrDefault(i => i >= n);
var low = list
.OrderByDescending(i => i)
.FirstOrDefault(i => i <= n);
Console.WriteLine(low);
Console.WriteLine(high);
results in 1, 33
结果1,33
the only issue is that if you specify 33 for n, you will get 33, 33. Is this correct? Can both be the same number? If not, you will have to limit your select to possibly "<=" and then just ">".
唯一的问题是,如果你为n指定33,你将获得33,33。这是正确的吗?两者都可以是相同的数字吗?如果没有,您必须将您的选择限制为可能“<=”然后只是“>”。
You should be able to adapt the solution to Linq to SQL.
您应该能够使解决方案适应Linq to SQL。
#3
ArrayList arrList = new ArrayList();
int number = 10;
arrList.Add(1);
arrList.Add(10);
arrList.Add(20);
arrList.Add(-12);
int numberBelow = (from int i in arrList
where i <= number
select i).Max();
int numberAbove = (from int i in arrList
where i >= number
select i).Min();
The number below is the maximum value less than your search value and likewise the number above is the minimum value greater than your search value. The only question is do you want greater than or equal or just greater than.
下面的数字是小于搜索值的最大值,同样上面的数字是大于搜索值的最小值。唯一的问题是你想要大于或等于或大于。
#1
Similar to Andrews answer, but I prefer doing the filter before the OrderBy, which reduces the amount of data the query has to run through. Also OrderBy.First is the same as Min, and OrderByDescending.First is the same as Max.
与Andrews的答案类似,但我更喜欢在OrderBy之前进行过滤,这样可以减少查询必须运行的数据量。 OrderBy.First也与Min相同,OrderByDescending.First与Max相同。
var high = list
.Where(i => i > n)
.DefaultIfEmpty()
.Min();
var low = list
.Where(i => i <= n)
.DefaultIfEmpty()
.Max();
#2
var list = new[] { 1, 33, 66, 100 };
var n = 33;
var high = list
.OrderBy(i=>i)
.FirstOrDefault(i => i >= n);
var low = list
.OrderByDescending(i => i)
.FirstOrDefault(i => i <= n);
Console.WriteLine(low);
Console.WriteLine(high);
results in 1, 33
结果1,33
the only issue is that if you specify 33 for n, you will get 33, 33. Is this correct? Can both be the same number? If not, you will have to limit your select to possibly "<=" and then just ">".
唯一的问题是,如果你为n指定33,你将获得33,33。这是正确的吗?两者都可以是相同的数字吗?如果没有,您必须将您的选择限制为可能“<=”然后只是“>”。
You should be able to adapt the solution to Linq to SQL.
您应该能够使解决方案适应Linq to SQL。
#3
ArrayList arrList = new ArrayList();
int number = 10;
arrList.Add(1);
arrList.Add(10);
arrList.Add(20);
arrList.Add(-12);
int numberBelow = (from int i in arrList
where i <= number
select i).Max();
int numberAbove = (from int i in arrList
where i >= number
select i).Min();
The number below is the maximum value less than your search value and likewise the number above is the minimum value greater than your search value. The only question is do you want greater than or equal or just greater than.
下面的数字是小于搜索值的最大值,同样上面的数字是大于搜索值的最小值。唯一的问题是你想要大于或等于或大于。