join子句中Expression之一的Type不正确。在连接调用中类型推断失败

时间:2013-05-29 06:44:34

标签: linq

尝试加入两个类型DiseaseSymptomMapping&的列表时,在下面的GetDiseaseBySymptoms方法中获取此错误症状。可以帮助更好地了解GetDiseaseBySymptoms代码出了什么问题。

注意:不要担心GetDiseaseBySymptoms方法的返回类型,一旦解决了这个问题,将会在以后注意这一点。

class Program
    {
        static void Main(string[] args)
        {
            Disease malaria = new Disease { ID = 1, Name = "Malaria" };
            Disease Cholera = new Disease { ID = 1, Name = "Cholera" };

            Symptom Fever = new Symptom { ID = 1, Name = "Fever" };
            Symptom Cough = new Symptom { ID = 2, Name = "Cough" };
            Symptom Shevering = new Symptom { ID = 3, Name = "Shevering" };

            List<DiseaseSymptomMapping> DiseaseDetails = new List<DiseaseSymptomMapping> {
                new DiseaseSymptomMapping{ ID=1,disease=malaria,symptom=Fever},
                new DiseaseSymptomMapping{ ID=2,disease=malaria,symptom=Shevering},
                new DiseaseSymptomMapping{ ID=3,disease=Cholera,symptom=Fever},
                new DiseaseSymptomMapping{ ID=4,disease=Cholera,symptom=Cough}
            };

            List<Symptom> symptoms = new List<Symptom> { Fever, Fever,Shevering };

            List<Disease> diseases = GetDiseaseBySymptoms(symptoms, DiseaseDetails);

            foreach (Disease disease in diseases)
            {
                Console.WriteLine("Disease Name :{0}", disease.Name);
            }

            Console.ReadLine();
        }

        class Disease
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

        class Symptom
        {
            public int ID { get; set; }
            public string Name { get; set; }

        }

        class DiseaseSymptomMapping
        {
            public int ID { get; set; }
            public Disease disease { get; set; }
            public Symptom symptom { get; set; }

        }



         static List<Disease> GetDiseaseBySymptoms(List<Symptom> symptoms,List<DiseaseSymptomMapping> DiseaseDetails)
         {
                 var querytmp = from diseasedetails in DiseaseDetails
                      join symp in symptoms on diseasedetails.symptom equals symp  in symptomsgrp
                      select new 
                      {
                         DiseaseName= diseasedetails.Name,
                         Symptoms=symptomsgrp
                      };

              foreach (var v in querytmp)
         {
             Console.WriteLine("{0}", v.DiseaseName);
         }
                            return new List<Disease>();
        }


    }

1 个答案:

答案 0 :(得分:1)

in symptomsgrp更改为into symptomsgrp。你可以通过改变

来摆脱错误
DiseaseName = diseasedetails.Name

DiseaseName = diseasedetails.disease.Name