none
how to find value in object list c#?

    Question

  • I have this class:

     

    List<Error> errorobject = new List<Error>();

     

    public class Error

    {

     

    string _vtype, _errortype, _flname, _elname;

     

    string _lnnumber, _desc;

     

    public Error(string vtype, string errortype, string flname, string elname, string lnnumber, string descrip)

    {

    _vtype = vtype;

    _errortype = errortype;

    _flname = flname;

    _elname = elname;

    _lnnumber = lnnumber;

    _desc = descrip;

    }

     

    public string ValidationType { get { return _vtype; } set { _vtype = value; } }

     

    public string ErrorType { get { return _errortype; } set { _errortype = value; } }

     

    public string FileName { get { return _flname; } set { _flname = value; } }

     

    public string ElementName { get { return _elname; } set { _elname = value; } }

     

    public string LineNumber { get { return _lnnumber; } set { _lnnumber = value; } }

     

    public string Description { get { return _desc; } set { _desc = value; } }

     

    }

    now  before adding an object to the list i want to check if any duplicate values exist......i.e..if (ValidationType,ErrorType,FileName,ElementName,Description) all are already present  ****excluding  (line number)**** .  then  i want to simply add to the existing object "line number" value like this.......

    previous line number=1000,  current line number =1022.

    {previous line number value should change to (1000,1022)}

     

    this is my object

    errorobject.Add(

    new Error(validationType, errorType, fileName, elementName, lineNumber, errorDescription));

     

    Monday, October 25, 2010 2:45 PM

Answers

  • Override the Equals(object) method in your error class:

    public Error

    {

      public override bool Equals(object obj) { // add logic here}

    }

    Now you can run the following code on your list before you add an object;

    bool mExists = false;

    foreach (object lObj in errorobject)

    {

        if(lObj.Equals([the error you want to add]))

        {

            mExists = true;

            break;

        }

    }

     

    if(!mExists)

    {

      this.errorobject.Add([the error you want to add]);

    }

     

    Note, it is recommended that if you override the Equals(object) method, then you should also overload the the == and != operators.  You don't have to, but this will ensure that you get consitant results.  You can overload the as such:

    public static operator ==(object left, object right){// code goes here};

    public static operator !=(object left, object right){// code goes here};

    Hope this helps.


    public enum Answers { Everything = 0x2A }
    Monday, October 25, 2010 3:08 PM
  • Hi,

    You can also use a Predicate delegate. And you can write a function to check whether an Error object is already present in your collection..

     

    List<Error> errorList = new List<Error>();
    
    bool ItemExist(Error error) {
          bool bRetVal = false;
    
          Error er = errorList.Find(delegate(Error _error) {
            if (_error.ValidationType == error.ValidationType &&
              _error.ErrorType == error.ErrorType &&
              _error.FileName == error.FileName &&
              _error.ElementName == error.ElementName &&
              _error.LineNumber == error.LineNumber &&
              _error.Description == error.Description) {
    
              return true;
            }
    
            return false;
    
          });
    
          if (er != null) {
            bRetVal = true;
          }
          
            
    
          return bRetVal;
        }

     

    And you can check like this :

    Error er = new Error("TYPE1", "ERRORTYPE2", "FILE1", "ELNAME1", "LNUMBER1", "DESC1");
          if (!ItemExist(er)) {
            errorList.Add(er);
          }



    Manjuke Fernando ~ http://www.manjuke.com
    • Marked as answer by lokendraj Tuesday, October 26, 2010 11:04 AM
    Monday, October 25, 2010 4:13 PM

All replies

  • Override the Equals(object) method in your error class:

    public Error

    {

      public override bool Equals(object obj) { // add logic here}

    }

    Now you can run the following code on your list before you add an object;

    bool mExists = false;

    foreach (object lObj in errorobject)

    {

        if(lObj.Equals([the error you want to add]))

        {

            mExists = true;

            break;

        }

    }

     

    if(!mExists)

    {

      this.errorobject.Add([the error you want to add]);

    }

     

    Note, it is recommended that if you override the Equals(object) method, then you should also overload the the == and != operators.  You don't have to, but this will ensure that you get consitant results.  You can overload the as such:

    public static operator ==(object left, object right){// code goes here};

    public static operator !=(object left, object right){// code goes here};

    Hope this helps.


    public enum Answers { Everything = 0x2A }
    Monday, October 25, 2010 3:08 PM
  • Another option is to use Linq, something like this:

    errorobject.Any(x=> x.validationType = newobject.ValidationType && x.ErrorType = newObject.ErrorType && ...)

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Emanuel, Varga Monday, October 25, 2010 3:27 PM
    Monday, October 25, 2010 3:11 PM
  • 2 sugestions then

    1. If you want to disallow duplicates then a collection that do not acepts duplicates natively might be best.
    2. In order to know when an object is the same as another object you need to explain that to c#.  By default in classes an object is equal to another object only if it is the same object regardless of the value of the fields inside it.  You might need to override this behavior if is not suited for you.  Finding a duplicate inside a collection might imply O(n) operation for insert unless ordered, in which case it will be O(lgn) to find and then the cost of inserting the element.

    Several method inside List allow for this, the easier being Contains

    http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

    However your object must implement IEquatable

    http://msdn.microsoft.com/en-us/library/ms131190.aspx

    Regards

    Monday, October 25, 2010 3:13 PM
  • Hi,

    You can also use a Predicate delegate. And you can write a function to check whether an Error object is already present in your collection..

     

    List<Error> errorList = new List<Error>();
    
    bool ItemExist(Error error) {
          bool bRetVal = false;
    
          Error er = errorList.Find(delegate(Error _error) {
            if (_error.ValidationType == error.ValidationType &&
              _error.ErrorType == error.ErrorType &&
              _error.FileName == error.FileName &&
              _error.ElementName == error.ElementName &&
              _error.LineNumber == error.LineNumber &&
              _error.Description == error.Description) {
    
              return true;
            }
    
            return false;
    
          });
    
          if (er != null) {
            bRetVal = true;
          }
          
            
    
          return bRetVal;
        }

     

    And you can check like this :

    Error er = new Error("TYPE1", "ERRORTYPE2", "FILE1", "ELNAME1", "LNUMBER1", "DESC1");
          if (!ItemExist(er)) {
            errorList.Add(er);
          }



    Manjuke Fernando ~ http://www.manjuke.com
    • Marked as answer by lokendraj Tuesday, October 26, 2010 11:04 AM
    Monday, October 25, 2010 4:13 PM
  • Thanx Manjuke.....

    thanx a lot

    i would recommend this as the best answer that fulfilled my needs.....

    thanx

    Tuesday, October 26, 2010 11:07 AM
  • Thanx Anubisascends

     

    Thanx to u sir.....your answer helped me develop my logic......

    I got the absolute code from Manjuke....Thanx once again

     

    Tuesday, October 26, 2010 11:09 AM