-3

I'm making a web service call and getting data back that I am adding to a list. No problems doing that. My remedyinfo list has content that I can verify with break points in VS.

I cannot seem to figure out how to search the list for a value that matches a new variable, for instance I want to find if incidentID is equal to "INC000000001".

I've tried var foundItem = remedyinfo.Contains("searchvalue") but it always returns false.

I've tried the LINQ queries as suggested in other post:

var foundItem = myArray.SingleOrDefault(item => item.intProperty == someValue);

What I do notice is that the sample refers to comparing item.intProperty == somevalue) should work, I am not able to get any reference after item., only suggested Equals, GetType, GetHashCode and ToString. So, I cannot reference item.incidentID for example.

Any guidance is greatly appreciated.

var remedyinfo = new List<object> { };

remedyinfo.Add(new IncidentItem()
  {
      assignedgroup = assignedgroup,
      incidentID = incidentID,
      submitdate = offsetDate.ToString(),
      priority = priority,
      status = status,
      assignee = assignee,
      summarydesc = summarydesc,
      notes = notes
  });

    [Serializable]
    public class IncidentItem
    {
        public string assignedgroup { get; set; }
        public string incidentID { get; set; }
        public string submitdate { get; set; }
        public string priority { get; set; }
        public string status { get; set; }
        public string assignee { get; set; }
        public string summarydesc { get; set; }
        public string notes { get; set; }
    }
NetMage
  • 26,163
  • 3
  • 34
  • 55
Frank M
  • 1,379
  • 9
  • 18

1 Answers1

0

To check if list contains an item, there are different options. You can use Any in .Net 3.5 or higher:

if (remedyinfo.Any(incident => incident.incidentID == "Hello"))
// rest of code

If you have another IncidentItem and wish to check in list like

var anotherIncident = new IncidentItem();
if (remedyInfo.Contains(anotherIncident)) 

then you either have to implement IEquatable or override Equals and HashCode in IncidentItem class. More on Equals and HashCode is here:

Correct way to override Equals() and GetHashCode()

Gauravsa
  • 6,330
  • 2
  • 21
  • 30