1

I have a C# list named AxiomSubset with 6 columns in which there are several rows of data and I would like to check if all the values of a particular column in the list are equal if they are equal then it should return true and go to the next column and check their equality and so on.

For Example, I have the following rows of data in the list

CC   Mon    str     stg  rate   units 

HP  15-Mar              4.0800  4
HP  15-Feb              4.0800  4
HP  15-Jan              4.0800  4
LN  15-Mar  3.25    Put 0.0500  50
LN  15-Feb  3.25    Put 0.0500  50
LN  15-Jan  3.25    Put 0.0500  50
LN  15-Mar  3.50    Put 0.1000  50
LN  15-Feb  3.50    Put 0.1000  50
LN  15-Jan  3.50    Put 0.1000  50

In the above data when checking the equality for column CC it should return false as they are all not equal

I tried doing it as follows which compares one row with the earlier row which doesn't give expected results obviously

for (int i = 0; i < AxiomSubSet.Count; i++)
{
    if (AxiomSubSet[i].CC.ToString() == AxiomSubSet[i + 1].CC.ToString())
    {
        result = true;
        if (AxiomSubSet[i].term.ToString() == AxiomSubSet[i + 1].ToString())
        {
            //So On
        }
    }
}

Above code compares two values at a time and will return true if they are equal without considering other values which is something unwanted.

Is there a better way to do it?

LightBulb
  • 964
  • 1
  • 11
  • 27
DoIt
  • 3,270
  • 9
  • 51
  • 103
  • Very few blocks of C# code `return` a value and continue to execute. What are you really trying to accomplish? – HABO Aug 13 '14 at 14:13
  • @FarhadJabiyev I think that compares values of all the columns in a row with the next row but I want to compare all the rows of a single column in list like comparing all the values of CC – DoIt Aug 13 '14 at 14:17

4 Answers4

5

You can do something like this for each column.

if((AxiomSubSet.Select(x => x.CC).Distinct().ToList()).Count > 1)
  • Is the tolist() necessary? – Davide Lettieri Aug 13 '14 at 14:25
  • 1
    I think this doesn't fully answer the question since it only checks the first column – Flat Eric Aug 13 '14 at 14:32
  • @Davide ToList() is necessary if you wish to use the .Count property. See: http://stackoverflow.com/questions/168901/howto-count-the-items-from-a-ienumerablet-without-iterating –  Aug 13 '14 at 14:40
  • @Flat Eric I believe the rest should be fairly straightforward; wrap this^ line in a foreach loop that goes through all the columns, is the first thing that comes to my mind. –  Aug 13 '14 at 14:42
  • @IlyaNemtsev is it better than the count() method? – Davide Lettieri Aug 13 '14 at 14:50
  • @Davide count() method will iterate like a 'for' loop if it does not know about the collection, so .Count should be equal or faster than .count(). –  Aug 13 '14 at 14:56
  • @IlyaNemtsev the tolist() enumerate the collection as well. I have a simple code to prove it. Now I can't post it but I will. – Davide Lettieri Aug 13 '14 at 15:05
  • Yes, tolist() will work in all cases! But, it is not the fastest method for collections. –  Aug 13 '14 at 15:07
1

I would think about using linq. Take the first value to find all matching values in the list, if the counts match between the lists all values are the same.

var listMatch = AxiomSubSet.Where(x => x.CC == AxiomSubSet[0].CC).Select(x => x);
result = listMatch.Count == AxiomSubSet.Count;

or more concise

if (AxiomSubSet.Where(x => x.CC == AxiomSubSet[0].CC).Select(x => x).Count == AxiomSubSet.Count)
bowlturner
  • 1,968
  • 4
  • 23
  • 35
1

You can change your IComparer as you would like but I am giving you here as a quick example:

public class AxiomSubset : IComparer<AxiomSubset>
{
    public int Compare(AxiomSubset x, AxiomSubset y)
    {
        if (x.CC.CompareTo(y.CC) != 0)
        {
            return x.CC.CompareTo(y.CC);
        }
        else if (x.term.CompareTo(y.term) != 0)
        {
            return x.term.CompareTo(y.term);
        }
        else
        {
            return 0;
        }
    }
}
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
1
from a in AxiomSubSet
group a by a.CC into g
select g.Count

You can group by the column, if the count is more than 1 you know there are different values. You can convert that to lambda, I prefer that syntax for readability.

AD.Net
  • 13,352
  • 2
  • 28
  • 47