I am fairly new to C#. I have been looking for a simple method to check if all the values in an array which are between or equal to lower and upper limits, these are different for each element in the array. I also run multiple evaluations on each dataset. This is the current method I use and this works fine, but I am looking to use a more eficient method. It seems that a pedicate method would appear to be along the lines of what I need, but I am unsure of how to get this to work.
There are also a couple of smaller questions at the end which lead on from this.
The arrays I use typically have around 20 elements, not 5 as here.
Column names have been changed to protect the innocent...
int x;
bool failed;
double [] myArray = new double [5];
for(int a = 0;a<= Convert.ToInt32(temp.Compute("Max(Col1)",string.Empty));a++)
{
failed = false;
IEnumerable<DataRow>rows = temp.AsEnumerable()
.Where(r=>r.Field<int>("Col1") == a
&& r.Field<int>("Col2") > 0
&& r.Field<int>("Col2") < 2000
&& r.Field<decimal>("Col3") > 0
&& r.Field<decimal>("Col3") < 1000
);
myArray[0] = rows.AsEnumerable().ToList().Count;
myArray[1] = rows.AsEnumerable().Sum(row => row.Field<int>("Col4"));
myArray[2] = rows.AsEnumerable().Average(row => row.Field<int>("Col4"));
myArray[3] = rows.AsEnumerable().Max(row => row.Field<int>("Col5"));
myArray[4] = rows.AsEnumerable().Min(row => row.Field<int>("Col5"));
//CHECK 1 ////////////////////////////////////////
double [] lowerLimit1 = {0,10,0,50000,2};
double[] upperLimit1 = {6000,20,0.75,80000,2};
for(int d = 0;d< myArray.Length;d++)
{
if(myArray[d] >= lowerLimit1[d] && myArray[d] <= upperLimit1[d])
{
x++;
};
};
if(x > myArray.Length)
{
failed = true;
dt = rows.CopyToDataTable<DataRow>();
};
//CHECK 2 ////////////////////////////////////////
if(! failed)
{
x = 0;
double [] lowerLimit2 = {6000,50,0,30000,2};
double[] upperLimit2 = {6000,10,0.75,50000,2};
for(int d = 0;d< myArray.Length;d++)
{
if(myArray[d] >= lowerLimit2[d] && myArray[d] <= upperLimit2[d])
{
x++;
};
};
if(x > myArray.Length)
{
failed = true;
dt = rows.CopyToDataTable<DataRow>();
};
};
//CHECK 3 ////////////////////////////////////////
if(! failed)
{
x = 0;
double [] lowerLimit3 = {4000,15,0,50000,2};
double[] upperLimit3 = {5000,20,0.75,80000};
for(int d = 0;d< myArray.Length;d++)
{
if(myArray[d] >= lowerLimit3[d] && myArray[d] <= upperLimit3[d])
{
x++;
};
};
if(x > myArray.Length)
{
failed = true;
dt = rows.CopyToDataTable<DataRow>();
};
};
}; //END LOOP
if(failed)
{
// Do stuff and things
};
Also is there a method to do the following without recreating the datarow object? This gives an error of unexpected symbol 'Where'.
myArray[4] = rows.AsEnumerable().Min(row => row.Field<int>("Col1") Where row.Field<int>("Col3") == 10);
I am trying to achieve the following, but using a data table is slower, which is why I use the above method.
myArray[4] = Convert.ToInt32(temp.Compute("Min(Col1)","Col3 = 10"));
And finally, is there a way to repopulate an array without a loop, I have tried array.Dispose() and array = null but they do not work. eg
double [] lowerLimit = {4000,15,0,50000,2};
double[] upperLimit = {5000,20,0.75,80000};
// Re-populate array which obviously does't work.
lowerLimit = {6000,50,0,30000,2};
upperLimit = {7000,60,0.75,50000,2};
which is much tidier than below or using multiple arrays as in the above code, as in use there are typically 20+ elements in the array.
lowerLimit[0] = 6000, upperLimit[0] = 7000;
lowerLimit[1] = 50, upperLimit[1] = 60;
lowerLimit[2] = 0, upperLimit[2] = 0.75;
lowerLimit[3] = 30000, upperLimit[3] = 50000;
lowerLimit[4] = 2, upperLimit[4] = 2;