简体   繁体   中英

get a particular property value using Linq to C#

I created a class with properties like this

public class Dependencies
{
    public string issueID { get; set; }
    public string status { get; set; }
    public int dependencyCheck { get; set; }
}

Now I created a method where i can use these properties.

private static void prepareIssuesList(string IssueKey, string JIRAtoken)
{
    Dependencies dpObj = new Dependencies();
    List<Dependencies> listObj = new List<Dependencies>();
    dpObj.issueID = IssueKey;
    dpObj.status = "open";
    dpObj.dependencyCheck = 0;
    listObj.Add(dpObj);

}

Now my question is, how to change the dependencyCheck property value. The prepareIssuesList() can called for multiple times. So i am adding multiple objects to Dependencies class. At certain point of time i want to change the dependencyCheck property value. How to do this. I think need to use the Linq to C#. ICan any one have any solution for this one?

I would do something along these lines:

public class Dependency
{
     public string IssueId { get; set; }
     public string Status { get; set; }
     public int DependencyCheck { get; set; }
}

public class DependencyManager
{
     public DependencyManager()
     {
          this.Dependencies = new List<Dependency>();
     }         

     public List<Dependency> Dependencies { get; private set; }

     public void AddDependency(string issueId)
     {
         var dep = new Dependency();
         dep.IssueId = issueId;
         dep.Status = "open";
         dep.DependencyCheck = 0;

         this.Dependencies.Add(dep);
     }

     public void SetDependencyCheck(string issueId, int value)
     {
         var dep = this.FindDependencyByIssueId(issueId);
         dep.DependencyCheck = value;
     }

     public Dependency FindDependencyByIssueId(string issueId)
     {
         var dep = this.Dependencies.FirstOrDefault(d => d.IssueId.Equals(issueId));
         if(dep == null) throw new ArgumentException("Dependency not found", "issueId");
         return dep;
     }
}

Then somewhere in your code you could do:

var mgr = new DependencyManager();
mgr.AddDependency("ABC123");
mgr.AddDependency("ABC456");

//... some other stuff that makes sense

mgr.SetDependecyCheck("ABC123", 42);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM