打印列表中的前6个结果

时间:2017-05-31 19:09:52

标签: c# list for-loop

我需要打印一个列表,该列表汇总了我使用

订购的患者列表中的前6个结果
 public enum PatientPriority { Magenta = 0, Red = 1, Yellow = 2, Green = 3 }; 

和我的军医名单。

我认为使用此代码是可行的:

foreach (var doctor in myDoctorList)
{
    if (sortedPatients.Count == 0)
        break; //no more patients

    var patient = sortedPatients[0];
    sortedPatients.RemoveAt(0); //remove the patient from the listing/queue

    doctor.AssignedPatient = patient;
}

我只是不确定如何检索" sortedPatients"。 这是我的军医名单。

        //MedicList
        MedicList.Add(new Medic(01, "Antonio", 555444333, "antonio@gmail.com", "Dermatologista"));
        MedicList.Add(new Medic(02, "Lucas", 555444333, "lucas78@gmail.com", "Cardiologista"));
        MedicList.Add(new Medic(03, "Duarte", 555444333, "Duarte90@gmail.com", "Otorrino"));
        MedicList.Add(new Medic(04, "Marcos", 555444333, "marcos123@gmail.com", "Clinica Geral"));
        MedicList.Add(new Medic(05, "Pedro", 555444333, "Pedro12@gmail.com", "Pediatra"));
        MedicList.Add(new Medic(06, "Pedro", 555444333, "Pedro12@gmail.com", "Urologia"));

这是使用随机颜色的My Pacient列表。

        // - ListaUtentes (presentes no Centro de Saúde)
        ListaUtente.Add(new Utente(100001, "Pedro", 914754123, "pedro@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100002, "Lucas", 974123214, "lucas91@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100003, "Rodrigo", 941201456, "rodrigo00@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100004, "Gaspar", 987453210, "gaspar@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100005, "Roberto", 974120219, "roberto@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100006, "Eduardo", 974120219, "edu@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100007, "Ismael", 974120219, "Isma@gmail.com", GetRandomColor()));
        ListaUtente.Add(new Utente(100008, "Paulo", 974120219, "Paulo90@gmail.com", GetRandomColor()));
        ListaUtente.Sort();

你能帮我解决这个问题吗?如果有人需要查看我的Medic或Utente(病人)类Atributes,我也可以发布它们。

3 个答案:

答案 0 :(得分:1)

以下是我认为您要问的问题,按照颜色顺序列出的前6位患者,假设洋红色是最高优先级,绿色最低。我还填写了你遗漏的一些东西,希望这会让你朝着正确的方向前进:我编造了Utente的属性,只是填充优先级颜色而不是随机生成它们。

using System.Collections.Generic;
using System;
using System.Linq;

public enum PatientPriority { Magenta = 0, Red = 1, Yellow = 2, Green = 3 }; 

public class Utente
{
    public int Id{get; set;}
    public string Name{get; set;}
    public int Key{get; set;}
    public string Email{get; set;}
    public PatientPriority Priority{get; set;}

    public Utente(int id, string name, int key, string email, PatientPriority priority)
    {
        Id = id;
        Name = name;
        Key = key;
        Email = email;
        Priority = priority;
    }

}

public class Test
{

    public static void Main()
{

        var ListaUtente = new List<Utente>(); 
        ListaUtente.Add(new Utente(100001, "Pedro", 914754123, "pedro@gmail.com", PatientPriority.Yellow));
        ListaUtente.Add(new Utente(100002, "Lucas", 974123214, "lucas91@gmail.com", PatientPriority.Green));
        ListaUtente.Add(new Utente(100003, "Rodrigo", 941201456, "rodrigo00@gmail.com", PatientPriority.Yellow));
        ListaUtente.Add(new Utente(100004, "Gaspar", 987453210, "gaspar@gmail.com", PatientPriority.Red));
        ListaUtente.Add(new Utente(100005, "Roberto", 974120219, "roberto@gmail.com", PatientPriority.Magenta));
        ListaUtente.Add(new Utente(100006, "Eduardo", 974120219, "edu@gmail.com", PatientPriority.Red));
        ListaUtente.Add(new Utente(100007, "Ismael", 974120219, "Isma@gmail.com", PatientPriority.Green));
        ListaUtente.Add(new Utente(100008, "Paulo", 974120219, "Paulo90@gmail.com", PatientPriority.Yellow));

        Console.WriteLine("Unsorted:");
        foreach (var u in ListaUtente)
        {
            Console.WriteLine("id: " + u.Id + " Priority: " + u.Priority);
        }

        Console.WriteLine("Sorted: ");
        foreach(var su in ListaUtente.OrderBy(u => u.Priority).Take(6).ToList())
        {
            Console.WriteLine("id: " + su.Id + " Priority: " + su.Priority);
        }

}
}

答案 1 :(得分:0)

var sortedPatients = ListaUtente.OrderBy(u => u.NameOfPriorityProperty).Take(6).ToList();

如果您想要所有患者(不仅仅是前6名),请删除Take步骤。

答案 2 :(得分:0)

如果您想将前6名患者分配到前6名医生,最好使用简单的...循环 列表类可以迭代,因为它是普通数组

int topCount = 6;
var sortedPatients = ListaUtente.OrderBy(x => x.Priority).ToList();
for(int x = 0; x < topCount; x++)
{
   MedicList[x].AssignedPatient  = sortedPatients[x];
   Console.WriteLine($"Utente:{sortedPatients[x].Name}, Priority:{sortedPatients[x].Priority}");
}

当然,这假设两个列表至少有6个条目