There is a collection

List<Plane> planesAll = PlaneCollection.GetPlane(); 

Selected position from event

 selectedPlaneBR = planes[e.Position].BR; 

So this BR in the collection can be from 1.0 to 10.0 and the options can be 1.0 , 1.3 , 1.7 , 2.0 , 2.3 , 2.7 , 3.0 , etc. up to 10.0 . It is necessary to make so that when choosing a user option, LINQ returns all collection objects with a spread of +-1 . That is, if you select 2.3 you must return the collection, in which there will be all the elements from 1.3 to 3.3 . This collection is transferred to the ListView . My method:

 var planevar = from p in planesAll where p.BR==selectedPlaneBR select p; planespotential = planevar.ToList<Plane>(); 

How to make all the conditions be fulfilled and the result that can be written in .ToList<T>() returned?

    2 answers 2

    I think that it is enough to make changes to the sample condition and you will get the desired result. For example:

     var planevar = from p in planesAll where (p.BR >= (selectedPlaneBR - 1)) && (p.BR <= (selectedPlaneBR + 1)) select p; 

    That is, select items in the selectedPlaneBR +-1 interval, just in case the conditions are enclosed in brackets for clarity. Useful links for familiarization:

    The second link was given, because I consider Method Syntax more convenient than Query Syntax. Look, read, I think it does not hurt. For example:

     var planevar = planesAll .Where(p => p.BR >= (selectedPlaneBR - 1) && p.BR <= (selectedPlaneBR + 1)) .ToList(); 
    • Really. Thank you very much. I tried it like this where p.BR == selectedPlaneBR - 1.0 && p.BR == selectedPlaneBR + 1.0 And it most likely looked for an argument that immediately fits two conditions - Bogdan Ilkiv
     using System; using System.Collections.Generic; using System.Linq; namespace Planes.Linq.Example { class Program { static void Main(string[] args) { var planesAll = new List<Plane>() { new Plane() { BR = 1.7 }, new Plane() { BR = 2.0 }, new Plane() { BR = 2.3 }, new Plane() { BR = 2.7 }, new Plane() { BR = 3.0 }, new Plane() { BR = 3.3 }, new Plane() { BR = 3.7 }, new Plane() { BR = 4.0 }, new Plane() { BR = 4.3 }, new Plane() { BR = 4.7 }, new Plane() { BR = 5.0 }, }; double selectedPlaneBR = 3.0; double step = 1.0; var selectedPlanes = from p in planesAll where p.BR <= selectedPlaneBR + step && p.BR >= selectedPlaneBR - step select p; foreach (var item in selectedPlanes) { Console.WriteLine(item); } Console.ReadKey(); } } public class Plane { public double BR { get; set; } public override string ToString() { return BR.ToString(); } } }