如何从不同的类初始化方法中的列表项?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从不同的类初始化方法中的列表项?相关的知识,希望对你有一定的参考价值。

我试图在一个不同类的方法返回的List中找到一个id或字符串。以下解决方案不起作用。我已经初始化了类,然后调用了方法,并且我正在尝试获取id。

public class RiskService
{
    private readonly Resource _matt = new Resource { Id = 1, Name = "Matt Sharpe" };
    private readonly Resource _john = new Resource { Id = 2, Name = "John Hillhouse" };
    private readonly Resource _julian = new Resource { Id = 3, Name = "Julian Jelfs" };
    private readonly Resource _darren = new Resource { Id = 4, Name = "Darren Thorpe" };
    private readonly Resource _jonm = new Resource { Id = 5, Name = "Jon Moore" };

    public List<Risk> GetRisks()
    {
        var risks = new List<Risk>();
        risks.Add(new Risk { Id = 1, Owner = _matt, RiskScore = 5, Status = RiskStatus.Approved, Title = "Lack Build Capacity In Dockyard" });
        risks.Add(new Risk { Id = 2, Owner = _john, RiskScore = 10, Status = RiskStatus.Unapproved, Title = "Small scale fire in warehouse" });
        risks.Add(new Risk { Id = 3, Owner = _matt, RiskScore = 17, Status = RiskStatus.Mitigated, Title = "Contract delays" });
        risks.Add(new Risk { Id = 4, Owner = _julian, RiskScore = 23, Status = RiskStatus.Open, Title = "Supplier insolvency" });
        risks.Add(new Risk { Id = 5, Owner = _darren, RiskScore = 13, Status = RiskStatus.Closed, Title = "Loss of key staff" });
        risks.Add(new Risk { Id = 6, Owner = _jonm, RiskScore = 97, Status = RiskStatus.Open, Title = "Fire in plant" });
        risks.Add(new Risk { Id = 7, Owner = _jonm, RiskScore = 97, Status = RiskStatus.Open, Title = "Fire in backup plant" });
        risks.Add(new Risk { Id = 8, Owner = _jonm, RiskScore = 45, Status = RiskStatus.Unapproved, Title = "Disaster recovery doesn't cover fire" });
        risks.Add(new Risk { Id = 9, Owner = _darren, RiskScore = 36, Status = RiskStatus.Approved, Title = "Component fails to meet performance" });
        risks.Add(new Risk { Id = 10, Owner = _john, RiskScore = 36, Status = RiskStatus.Approved, Title = "Component fails to meet performance" });
        return risks;
    }
}

要查找Jon Moore拥有的风险,这是我的解决方案:

public class Solution
{
    static void Main(string[] args)
    {
        RiskService riskService = new RiskService();
        Console.WriteLine(riskService.GetRisks().ElementAt(8));                              
    }       
}
答案

试试这个:

static void Main(string[] args)
{
    RiskService riskService = new RiskService();
    var result = riskService.GetRisks().FirstOrDefault(x => x.Id == 8);
    Console.WriteLine(result?.Owner.Name);
}
另一答案

要查找Jon Moore(1)拥有的风险,您可以使用Linq Where过滤您的List<Risk>。 在谓词中导航到想要的属性Risk> Owner> MyProperty。

var risks = new RiskService().GetRisks().Where(x=> x.Owner.Name=="Jon Moore");

foreach(var risk in risks){
    //  risk.Dump();
    Console.WriteLine("Id:"+risk.Id+" OwnerID:"+risk.Owner.Id+" OwnerName:"+risk.Owner.Name+" Title:"+risk.Title);
}

这是一个工作例子:DotNetFiddle


(1): From a comment in The original question

以上是关于如何从不同的类初始化方法中的列表项?的主要内容,如果未能解决你的问题,请参考以下文章

从活动中调用片段事件

构造函数初始值设定项列表中长度未知的数组

OnItemClick 如何从列表视图中获取单击项目的文本值

根据字符串名称而不是列表视图项位置替换片段并启动活动

片段 A 的列表视图中的片段 B 中的新列表视图,单击 A 的列表项

将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener