gpt4 book ai didi

c# - 使用 Linq to C# 获取特定属性值

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:04 25 4
gpt4 key购买 nike

我创建了一个具有这样属性的类

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

现在我创建了一个可以使用这些属性的方法。

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);

}

现在我的问题是,如何更改 dependencyCheck 属性值。 prepareIssuesList() 可以调用多次。所以我将多个对象添加到 Dependencies 类。在某个时间点,我想更改 dependencyCheck 属性值。这个怎么做。我认为需要使用 Linq to C#。 ICan anyone have any solution for this one吗?

最佳答案

我会按照这些思路做一些事情:

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;
}
}

然后在你的代码中的某处你可以这样做:

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

//... some other stuff that makes sense

mgr.SetDependecyCheck("ABC123", 42);

关于c# - 使用 Linq to C# 获取特定属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com