LINQ foreach заявление, чтобы получить элементы не работают - PullRequest
0 голосов
/ 06 декабря 2018
public partial class Form1 : Form
{
    List<Doctor> doctors = new List<Doctor>();
    List<Episode> episodes = new List<Episode>();
    List<Companion> companions = new List<Companion>();
    public Form1()
    {
        InitializeComponent();
        doctorCombo.Enabled = false;
    }

    private void openMenu_Click(object sender, EventArgs e)
    {
        String input;
        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            //Give a title to the popup and a filter for what files to open.  Then opens the created dialogue
            openFileDialog.Title = "Browse Text Files";
            openFileDialog.Filter = "Text|*.txt"; ;
            openFileDialog.ShowDialog();

            //Read the contents of the file into a stream
            var fileStream = openFileDialog.OpenFile();

            //Opens a streamreader
            using (StreamReader reader = new StreamReader(fileStream))
            {
                while ((input = reader.ReadLine()) != null)
                {
                    String[] exploded = input.Split('|');
                    if (exploded[0].Contains("D "))
                    {
                        Doctor d = new Doctor(Convert.ToInt32(exploded[1]), exploded[2], Convert.ToInt32(exploded[3]), Convert.ToInt32(exploded[4]), exploded[5]);
                        doctors.Add(d);
                    }
                    if (exploded[0].Contains("E "))
                    {
                        Episode ep = new Episode(exploded[1], Convert.ToInt32(exploded[2]), Convert.ToInt32(exploded[3]), exploded[4]);
                        episodes.Add(ep);
                    }
                    if (exploded[0].Contains("C "))
                    {
                        Companion c = new Companion(exploded[1], exploded[2], Convert.ToInt32(exploded[3]), exploded[4]);
                        companions.Add(c);
                    }
                }
                reader.Close();
            }
            doctorCombo.Enabled = true;
            doctorCombo.SelectedIndex = 0;
            var doctorsCompanion =
                from currentDoctor in doctors
                join currentCompanion in companions on currentDoctor.debut equals currentCompanion.debut
                select new {currentDoctor.actor, currentDoctor.series, currentDoctor.age, currentCompanion.debut, currentCompanion.name, currentCompanion.actor };


            foreach (var item in doctorsCompanion)
            {
                outputList.Items.Add("");
            }

Оператор foreach в конце здесь повторяет, что is не может работать с переменными типа '?'так как '?'не содержит общедоступного определения экземпляра 'getenumerator'. Я объединяю класс с именем doctor с классом под названием companion.

1 Ответ

0 голосов
/ 06 декабря 2018

У вас есть два actor члена в вашем анонимном типе:

select new {currentDoctor.actor, /* ... */ currentCompanion.actor };

Это ошибка, поэтому компилятор не может продолжать.

Вы должны дать им разные имена:

select new { doctor_actor = currentDoctor.actor, /* ... */ 
    companion_actor = currentCompanion.actor };
...