开发者

Filter List<Point> as per given Condition using LINQ

One Generic list that contain Points say =>List<Point>

Now I need to filter points and store it in another List<Point> as per below condition and Using LINQ.

• At given Y value of Point

• Find 开发者_开发知识库Points with Maximum X values and Minimum X values.

NOTE:

Actually friends, I am looking for the LINQ query which performs above operation in just single query if possible.

Otherwise any best solution for above operation using LINQ,

EDIT:- see my code here but i am looking for sort solution.....

               int givenY = 147;

                List<Point> listOfPointLocal = (from point in listOfPointMain
                        where point.Y == givenY
                        select point).ToList();

                var minX = listOfPointLocal.Min(p => p.X);
                var maxX = listOfPointLocal.Max(p => p.X);

                List<Point> listOfFilterdPoint = (from p in listOfPointLocal
                                         where p.X <= minX || p.X >= maxX
                                         select p).ToList();

Thanks…..


With a little help from an extra class you can pass the list only once.

public class MaxMin
{
    public int max { get; set; }
    public int min { get; set; }
}

And then

var points = new List<Point>() 
                {
                    new Point(10, 10), 
                    new Point(15, 10), 
                    new Point(20, 10), 
                    new Point(42, 42), 
                    new Point(47, 11) 
                };

var maxmin = new MaxMin() { max = int.MinValue, min = int.MaxValue};
maxmin = points.Where(p => p.Y == 10).
                    Aggregate(maxmin, (acc, next) =>
                    {
                        if (next.X > acc.max) acc.max = next.X;
                        if (next.X < acc.min) acc.min = next.X;
                        return acc;
                    });


to keep it a bit readable:

 var points = new List<Point>()
                    {
                        new Point(10, 10), 
                        new Point(15, 10), 
                        new Point(20, 10), 
                        new Point(42, 42), 
                        new Point(47, 11) 
                    };

var maxes = new {
    minX  = (from p in points orderby p.X select p).First(),  // with lowest x
    maxX  = (from p in points orderby p.X select p).Last(), // with highest x
    minY  = (from p in points orderby p.Y select p).First(), //  with lowest y
    maxY  = (from p in points orderby p.Y select p).Last() // with highest y
};


Although it does not give you min and max X in single query, following code may be useful to you.

        List<Point> list = new List<Point>();
        list.Add(new Point(10, 10));
        list.Add(new Point(15, 10));
        list.Add(new Point(20, 10));
        list.Add(new Point(25, 15));
        list.Add(new Point(30, 15));

        var minX = (from p in list
                    where (p.Y.Equals(10))
                    select p.X).Min();

        var maxX = (from p in list
                    where (p.Y.Equals(10))
                    select p.X).Max();
        Console.WriteLine(minX.ToString());
        Console.WriteLine(maxX.ToString());
        Console.ReadLine();


This will return the one result set, but it is still iterating the list twice, there is no way you can do it without that though AFAIK.

list.Select(a => new 
 { 
 Max = list.Where(max => max.Y == ???).Max(max => max.X), 
 Min = list.Where(min => min.Y == ???).Min(min => min.X)
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜