【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<DataLibrary.PLANT>'无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.List<DataLibrary.PLANT>”
【发布时间】:2015-05-24 21:38:45
【问题描述】:

我一直在努力尝试使用 EntityFramework LINQ 查询进行第一次 JOIN。当我在没有连接的情况下创建此查询时,我没有收到错误消息。我需要做什么?

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

【问题讨论】:

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


    【解决方案1】:

    您正在选择一种新的匿名类型。而是选择新的 PLANT 对象

    select new PLANT
     {
        //set values here
    
     }).ToList();
    

    edit* 只是注意到您想从 BLOOM 中引入一个值 - 在这种情况下,您可以为 PLANT 类创建一个新的非映射属性以用于 Bloom desc,或者您可以创建一个包含 BLOOM 和 PLANT 属性的新类然后选择它。

    【讨论】:

    • 我收到错误:无法使用集合初始化程序初始化类型“DataLibrary.PLANT”,因为它没有实现“Systems.Collections.IEnumerable”
    • 确保在选择新的 PLANT 对象msdn.microsoft.com/en-us/library/bb384062.aspx时使用对象初始化
    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2011-05-14
    • 2013-05-15
    相关资源
    最近更新 更多