从列表中检索项目 [英] Retrieving item from list

查看:40
本文介绍了从列表中检索项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从列表中检索信息时遇到一些问题.

I'm having some problems retrieving information from a list.

在这种情况下,我想从列表中获取名称,最终我希望将其转换为字符串.这就是我所拥有的:

In this case I'd like to get the name from a list, eventually I want it turned into a string. This is what I have:

public string ShowName(int patientcode)
{
    List<ExtendPatientInfo> patientdata = dk.RetrieveList(patientcode);
    string name = patientdata. <What here?>
    return name;
}

在ExtendPatientInfo类中,我有这个,我认为还可以:

In the class ExtendPatientInfo I have this, which I think is allright:

private string name;
public ExtendPatientInfo(string name)
{
    this.name = name;
}

public string Name
{
    get
    {
        return name;
    }
}

我尝试使用一些东西.像包含,查找,FindIndex和位置.但是这些都不对我有用,因为我可能在某个地方弄乱了东西.有人可以帮助我吗?

I tried using a few things. Like Contains, Find, FindIndex and where. But none of these worked for me because I probably messed something up somewhere. Anybody that can help me?

推荐答案

您必须选择患者.如果对于患者来说所有数据都相同,那么您可以使用LINQ的First

You have to choose the patient. If it is all the same data for the patient, then you can use LINQ's First

patientdata.First().Name

但是,如果它们都不同,则可以将列表映射为仅具有名称

But, if they are all different, then you could map the list to only have Names

patientdata.Select(x=>x.Name)

这时,您仍然需要遍历列表以显示每个名称或所需的名称.

At which point, you would still need to iterate through the list to display each name or whatever you need.

正如Henk指出的那样,如果这始终是一项的列表,那么您可以使用

As Henk points out, if this is always going to be a list of one item, then you could use

patientdata.Single().Name

*带有 Single 的警告来自MSDN

*The caveat with Single is from MSDN

返回序列中的唯一元素,如果序列中不存在一个元素,则抛出异常.

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

这篇关于从列表中检索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆