Reputation: 5836
Say I have a list with random values
11, 22, 33, 44, 55, 66, 77, 88
I want to extract all values between 20-60
lets say which uses the value lets say 40
with offsets -20
and +20
.
so I should get 22,33,44,55
only
Code
uint Val = 40;
List<uint> List1 = new List<uint>() {11, 22, 33, 44, 55, 66, 77, 88};
List<uint> CheckedList = List1.Where(t => (Val - 20) >= t && t <= (Val + 20));
I tried this code and I cannot get it to compile Linq is very difficult to grasp.
Upvotes: 0
Views: 74
Reputation: 36
List<uint> CheckedList = List1.Where(t => t>= (Val - 20) && t <= (Val + 20)).ToList();
Result: 22,33,44 and 55.
Upvotes: 1
Reputation: 460350
Append ToList
if you want a list:
List<uint> CheckedList = List1.Where(t => t >= (Val - 20) && t <= (Val + 20)).ToList();
Upvotes: 6
Reputation: 11238
LINQ methods mostly return IEnumerable<T>
, to get a List
you can either use ToList
method:
List<uint> CheckedList = List1.Where(t => (Val - 20) >= t && t <= (Val + 20))
.ToList();
or call a List constructor that takes IEnumerable<T>
:
List<uint> CheckedList = new List<uint>(list1.Where(t => (Val - 20) >= t && t <= (Val + 20)));
Upvotes: 1
Reputation: 350
You can Try this
List<uint> rangeList=new List<uint>();
foreach(var values in List1)
{
if(values>=(val-20) && values<=(val+20))
{
rangeList.Add(values);
}
}
Upvotes: 1