Cannot implicitly convert type &#39;System.Collections.Generic.List<anonymoustype#1>&#39; to &#39;System.Collections.Generic.List<datalibrary.plant>&#39;

时间:2015-05-24 21:38:46

标签: c# entity-framework join linq-to-entities

I have worked on trying to get this first attempt at a JOIN with the EntityFramework LINQ query. When I create this query without the join, I don't get the error. What do I need to do?

public List<PLANT> getPlantDetails(int plantid)
{
    List<PLANT> plantDetails = (from plant in db.PLANTs
                                join bloom in db.BLOOMs on plant.PLANT_ID equals bloom.PLANT_ID
                                where plant.PLANT_ID == bloom.PLANT_ID
                                && plant.PLANT_ID == plantid
                                select new
                                {
                                    plant.PLANT_ID,
                                    plant.PL_GENUS,
                                    plant.PL_SPECIES,
                                    plant.PL_NAME,
                                    plant.PL_DESC,
                                    plant.PL_HEIGHT,
                                    plant.PL_SPACING,
                                    plant.PL_IMAGE,
                                    plant.PL_IMAGE_THUMB,
                                    bloom.BLOOM_DESC
                                }).ToList();

    return plantDetails;
}

1 个答案:

答案 0 :(得分:2)

you are selecting a new anonymous type. Instead, select new PLANT object

select new PLANT
 {
    //set values here

 }).ToList();

edit* just noticing you want to bring in a value from BLOOMs - in that case you can maybe create a new non-mapped property to the PLANT class for bloom desc or you can create a new class which holds BLOOM and PLANT properties and then select that instead.

相关问题