[英]Linq c# sum any objects from list > int
I want do Linq like in topic.我想做Linq就像主题一样。 I have
List<double>
i want to check there is 2
different double
which sum
is more then for example 20
.我有
List<double>
我想检查是否有2
不同的double
, sum
例如20
。 i don't have idea how to do that.我不知道该怎么做。 I create something like this
我创造了这样的东西
tags.Any(x => (x.Distance + tags.Select(y => y.Distance).First() > 20));
But it check only with first object, and check with myself.但它只检查第一个object,然后自己检查。 If
2
elements on list are the same they can be check, but with myself can't.如果列表中的
2
元素相同,则可以检查它们,但我自己不能。
Can i do something like take this and next object from list?我可以从列表中获取这个和下一个 object 吗?
var tags = new List<double>() {
5, 7, 9, 13
};
if ((i != j) && (tags(i) + tags(j)) > 20)
{
}
This link should work like this if
when it would be array in a loop . if
它是循环中的数组,则该链接应该像这样工作。
Try Cartesian Join ( tags
with itself ):尝试笛卡尔连接(带有自身的
tags
):
var tags = new List<double>() {
5, 7, 9, 13
};
var result = tags
.Select((a, index_a) => tags
.Where((b, index_b) => index_a != index_b && a + b > 20)
.Select(b => Tuple.Create(a, b)))
.SelectMany(item => item);
// Let's have a look:
Console.Write(string.Join(Environment.NewLine, result));
Outcome:结果:
(9, 13)
(13, 9)
if you don't want (13, 9)
and alike pairs (where index_a > index_b
)如果您不想要
(13, 9)
和类似的对(其中index_a > index_b
)
var result = tags
.Select((a, index_a) => tags
.Skip(index_a + 1)
.Where(b => a + b > 20)
.Select(b => Tuple.Create(a, b)))
.SelectMany(item => item);
// Let's have a look:
Console.Write(string.Join(Environment.NewLine, result));
Outcome:结果:
(9, 13)
Linq is a fancy cover for for loops, foreach loops and conditional statements, as result I would construct the above question in terms of for loops and conditionals etc, then afterwards look to convert this into a Linq statement. Linq 是 for 循环、foreach 循环和条件语句的精美封面,因此我将根据 for 循环和条件等构造上述问题,然后将其转换为 Linq 语句。
eg例如
private List<Tuple<double, double>> GetElementsWhosSumIsGreaterThanX(List<double> originalList, double x)
{
//take the absolute value of the variable to eliminate sign errors
x= Math.Abs(x);
List<Tuple<double, double>> returnList = new List<Tuple<double, double>>();
//only go upto count - 1 as there is no more elements to compare the last element to
for (int i = 0; i < originalList.Count - 1; i++)
{//loop over each element in turn
for (int j = i; j < originalList.Count - i; j++)
{//compare it to the rest of the list from the ith element on
double sum = originalList[i] + originalList[j];
if (sum> x)
returnList.Add( new Tuple<double, double>(originalList[i], originalList[j]));
}
}
return returnList;
}
This can be used like:这可以像这样使用:
public void Test()
{
List<double> tags = new List<double>() {5, 7, 9, 13, 10.1, 25.6, 32.1};
List<Tuple<double, double>> results = GetElementsWhosSumIsGreaterThanX(tags, 20);
for (int i = 0; i < results.Count; i++)
{
Console.WriteLine("{0} and {1} have a sum greater than 20", results[i].Item1, results[i].Item2);
}
/*output:
5 and 25.6 have a sum greater than 20
5 and 32.1 have a sum greater than 20
7 and 25.6 have a sum greater than 20
9 and 13 have a sum greater than 20
13 and 13 have a sum greater than 20
*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.