R
RayLopez99
Please examine the below code. Kindly explain why .Select does not
work except to produce a 'bool' and further, how can you extract which
member is true/false?
This is the complete code (Jon).
I used .Where to get the information I need (namely, finding a maximum
point in an array of Points, then finding all points less than this
maximum point). Why could not I use .Select to do this? Select seems
useless.
RL
//Windows Forms so for points use using System.Windows.Forms; and
using.System.Linq;
/// find the maximum Point in an array of Points, then find all points
that are less than this maximum point.
Point[] PtsInArray = { new Point(10, 10), new Point(20, 1), new Point
(1,2), new Point(2,2) };
int Max = PtsInArray.Max(ImplicitDummyVar =>
ImplicitDummyVar.X + ImplicitDummyVar.Y);
int Min = PtsInArray.Min(x => x.X + x.Y);
Console.WriteLine("Here are the Max, Min: Max= {0}, Min=
{1}", Max, Min);
// IEnumerable<Point> i1 = PtsInArray.Select(x => (x.X + x.Y) >
Min); //won't compile, why?
var MinPtsArr2 = PtsInArray.Select(x => (x.X + x.Y) >
Min);
foreach (var i2 in MinPtsArr2)
{
Console.WriteLine("data is: {0}", i2);
}
IEnumerable<Point> SomePtsArr3 = PtsInArray.Where(x => (x.X
+ x.Y) < Max);
foreach (Point p in SomePtsArr3)
{
Console.WriteLine("Works, point p less than {0}, is:
{1},{2}", Max, p.X, p.Y);
}
/* //output
*
* Here are the Max, Min: Max= 21, Min= 3
data is: True
data is: True
data is: False
data is: True
Works, point p less than 21, is: 10,10
Works, point p less than 21, is: 1,2
Works, point p less than 21, is: 2,2
*
*
* */
work except to produce a 'bool' and further, how can you extract which
member is true/false?
This is the complete code (Jon).
I used .Where to get the information I need (namely, finding a maximum
point in an array of Points, then finding all points less than this
maximum point). Why could not I use .Select to do this? Select seems
useless.
RL
//Windows Forms so for points use using System.Windows.Forms; and
using.System.Linq;
/// find the maximum Point in an array of Points, then find all points
that are less than this maximum point.
Point[] PtsInArray = { new Point(10, 10), new Point(20, 1), new Point
(1,2), new Point(2,2) };
int Max = PtsInArray.Max(ImplicitDummyVar =>
ImplicitDummyVar.X + ImplicitDummyVar.Y);
int Min = PtsInArray.Min(x => x.X + x.Y);
Console.WriteLine("Here are the Max, Min: Max= {0}, Min=
{1}", Max, Min);
// IEnumerable<Point> i1 = PtsInArray.Select(x => (x.X + x.Y) >
Min); //won't compile, why?
var MinPtsArr2 = PtsInArray.Select(x => (x.X + x.Y) >
Min);
foreach (var i2 in MinPtsArr2)
{
Console.WriteLine("data is: {0}", i2);
}
IEnumerable<Point> SomePtsArr3 = PtsInArray.Where(x => (x.X
+ x.Y) < Max);
foreach (Point p in SomePtsArr3)
{
Console.WriteLine("Works, point p less than {0}, is:
{1},{2}", Max, p.X, p.Y);
}
/* //output
*
* Here are the Max, Min: Max= 21, Min= 3
data is: True
data is: True
data is: False
data is: True
Works, point p less than 21, is: 10,10
Works, point p less than 21, is: 1,2
Works, point p less than 21, is: 2,2
*
*
* */