开发者

Linq Retrieve Entities using a Navigational Property

开发者 https://www.devze.com 2023-03-24 09:36 出处:网络
Linq and EF. I\'m pretty new so I have some problem to retrieve entities using a Navigational Property (CmsContents).

Linq and EF.

I'm pretty new so I have some problem to retrieve entities using a Navigational Property (CmsContents). I can return as an List but not as an IEnumerable.

  • Could you tell me what is wrong in my code?
  • Also do you know a better approach to retrieve Entities suing Navigational Properties?

Please provide me an example of code thanks!

    public IEnumerable<CmsGroupsType> GetMostPopularContents()
    {
        using (var context = new CmsConnecti开发者_JS百科onStringEntityDataModel())
        {
            context.CmsGroupsTypes.MergeOption = MergeOption.NoTracking;
            var contents = context.CmsGroupsTypes.Single(g => g.GroupTypeId == 1).CmsContents;
            return contents.ToList();                                         
        }
    }

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<WebProject.DataAccess.DatabaseModels.CmsContent>' to 'System.Collections.Generic.IEnumerable<WebProject.DataAccess.DatabaseModels.CmsGroupsType>'. An explicit conversion exists (are you missing a cast?)


The generic types don't match: Your .ToList() is of CmsContent, but your return type is an IEnumerable of CmsGroupsType. I'm not sure if that was intentional, but changing the return type to IEnumerable<CmsContent> will make everything work.


Change your return type from CmsGroupsType to WebProject.DataAccess.DatabaseModels.CmsContent

0

精彩评论

暂无评论...
验证码 换一张
取 消