简体   繁体   English

从列表中检索项目

[英]Retrieving item from list

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

In the class ExtendPatientInfo I have this, which I think is allright: 在ExtendPatientInfo类中,我认为这没问题:

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

public string Name
{
    get
    {
        return name;
    }
}

I tried using a few things. 我尝试使用一些东西。 Like Contains, Find, FindIndex and where. 像包含,查找,FindIndex和位置。 But none of these worked for me because I probably messed something up somewhere. 但是这些都不对我有用,因为我可能在某个地方弄乱了东西。 Anybody that can help me? 有人可以帮助我吗?

You have to choose the patient. 您必须选择患者。 If it is all the same data for the patient, then you can use LINQ's First 如果对于患者来说所有数据都相同,则可以使用LINQ的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. 此时,您仍然需要遍历列表以显示每个名称或所需的名称。


As Henk points out, if this is always going to be a list of one item, then you could use 正如Henk所指出的,如果这始终是一项的列表,那么您可以使用

patientdata.Single().Name

*The caveat with Single is from MSDN *有关Single的警告来自MSDN

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. 返回序列中的唯一元素,如果序列中不存在一个元素,则引发异常。

Well you have a list of object and you want to show properties of each object, then you have to access each object in the list and then access it properties. 好了,您有一个对象列表,并且想要显示每个对象的属性,然后必须访问列表中的每个对象,然后访问它的属性。 Something like: 就像是:

foreach(var item in patientdata)
{
  Console.WriteLine(item.Name);
  //rest of the fields
}

If you want to select a single object from your list then you cause use First / FirstOrDefault , Single / SingleOrDefault depending on your need. 如果要从列表中选择单个对象,则根据需要使用First / FirstOrDefaultSingle / SingleOrDefault But you need an individual object to access it properties. 但是您需要一个单独的对象来访问它的属性。 You can't access a property directly from a list. 您不能直接从列表访问属性。

var item = patientData.FirstOrDefault();
if(item != null)
   Console.WriteLine(item.Name);

Try 尝试

string name = string.Empty;
var pd = patientData.FirstOrDefault();
if(pd != null)
     name = pd.Name

This code gets the first item returned or null. 此代码获取返回的第一项或为null。 If it isn;t null it retrieves the Name property value into string variable name. 如果不为null,则将Name属性值检索为字符串变量名称。

Incidentally, if you don;t want to learn linq right now you can access List<> via index like: 顺便说一句,如果您现在不想学习linq,则可以通过类似以下内容的索引访问List <>:

 patientData[0].Name

You will still want to check that patientData[0] is not null before checking the Name property. 在检查Name属性之前,您仍然需要检查PatientData [0]是否不为null。

You probably want to use Linq . 您可能要使用Linq Linq allows you to cycle through a list easily and get what you want. Linq使您可以轻松地遍历列表并获得所需的内容。

For example, if you want the patient named "Johan": 例如,如果您想要名为“ Johan”的患者:

ExtendPatientInfo patient = patientdata.Where(x => x.Name == "Johan").FirstOrDefault();
string name = patient.Name;

If I understand your question, you are looking for something like this 如果我理解您的问题,那么您正在寻找这样的东西

ExtendPatientInfo patient = 
         patientdata.FirstOrDefault(x => x.patientcode == patientcode);

return 
    (patient != null)
        ? patient.Name
        : null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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