EF单一实体问题

发布于 2024-11-01 10:39:27 字数 1514 浏览 1 评论 0原文

我需要从存储库返回视图模型类的单个实例,以便将其输入强类型视图

在我的存储库中,这对于视图模型实例的集合来说效果很好:

    IEnumerable<PAWeb.Domain.Entities.Section> ISectionsRepository.GetSectionsByArea(int AreaId)
    {
        var _sections = from s in DataContext.Sections where s.AreaId == AreaId orderby s.Ordinal ascending select s;

        return _sections.Select(x => new PAWeb.Domain.Entities.Section()
        {
            SectionId = x.SectionId,
            Title = x.Title,
            UrlTitle = x.UrlTitle,
            NavTitle = x.NavTitle,
            AreaId = x.AreaId,
            Ordinal = x.Ordinal
        }
        );
    }

但是当我尝试获取单个实体时,例如this:

    public PAWeb.Domain.Entities.Section GetSection(int SectionId)
    {
        var _section = from s in DataContext.Sections where s.SectionId == SectionId select s;

        return _section.Select(x => new PAWeb.Domain.Entities.Section()
        {
            SectionId = x.SectionId,
            Title = x.Title,
            UrlTitle = x.UrlTitle,
            NavTitle = x.NavTitle,
            AreaId = x.AreaId,
            Ordinal = x.Ordinal
        }
        );
    }       

我明白

Error   1   Cannot implicitly convert type  
'System.Linq.IQueryable<PAWeb.Domain.Entities.Section>' to 
'PAWeb.Domain.Entities.Section'. An explicit conversion exists 
(are you missing a cast?)"

这必须很简单,但我是 c# 新手,我无法弄清楚转换。我在不同的地方尝试过(PAWeb.Domain.Entities.Section),但没有成功。有人可以帮忙吗?

I need to return a single instance of my viewmodel class from my repository in order to feed this into a strongly-typed view

In my repository, this works fine for a collection of viewmodel instances:

    IEnumerable<PAWeb.Domain.Entities.Section> ISectionsRepository.GetSectionsByArea(int AreaId)
    {
        var _sections = from s in DataContext.Sections where s.AreaId == AreaId orderby s.Ordinal ascending select s;

        return _sections.Select(x => new PAWeb.Domain.Entities.Section()
        {
            SectionId = x.SectionId,
            Title = x.Title,
            UrlTitle = x.UrlTitle,
            NavTitle = x.NavTitle,
            AreaId = x.AreaId,
            Ordinal = x.Ordinal
        }
        );
    }

But when I attempt to obtain a single entity, like this:

    public PAWeb.Domain.Entities.Section GetSection(int SectionId)
    {
        var _section = from s in DataContext.Sections where s.SectionId == SectionId select s;

        return _section.Select(x => new PAWeb.Domain.Entities.Section()
        {
            SectionId = x.SectionId,
            Title = x.Title,
            UrlTitle = x.UrlTitle,
            NavTitle = x.NavTitle,
            AreaId = x.AreaId,
            Ordinal = x.Ordinal
        }
        );
    }       

I get

Error   1   Cannot implicitly convert type  
'System.Linq.IQueryable<PAWeb.Domain.Entities.Section>' to 
'PAWeb.Domain.Entities.Section'. An explicit conversion exists 
(are you missing a cast?)"

This has got to be simple, but I'm new to c#, and I can't figure out the casting. I tried (PAWeb.Domain.Entities.Section) in various places, but no success. Can anyone help??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2024-11-08 10:39:27

您的查询返回一个 IQueryable,其中可能有多个项目。例如,考虑对象数组或列表与单个对象之间的区别。它不知道如何将 List 转换为单个对象,应该选择哪一个?第一个?最后一个?

你需要明确告诉它只拿一件物品。

例如,

public PAWeb.Domain.Entities.Section GetSection(int SectionId)
    {
        var _section = from s in DataContext.Sections where s.SectionId == SectionId select s;

        return _section.Select(x => new PAWeb.Domain.Entities.Section()
        {
            SectionId = x.SectionId,
            Title = x.Title,
            UrlTitle = x.UrlTitle,
            NavTitle = x.NavTitle,
            AreaId = x.AreaId,
            Ordinal = x.Ordinal
        }
        ).FirstOrDefault();
    }  

这将返回第一个项目,如果没有与您的查询匹配的项目,则返回 null。在您的情况下,除非表为空,否则不会发生这种情况,因为您没有 where 子句。

Your query is returning an IQueryable, which could have several items. For example, think of the difference between an Array or List of objects and a single object. It doesn't know how to convert the List to a single object, which one should it take? The first? The last?

You need to tell it specifically to only take one item.

e.g.

public PAWeb.Domain.Entities.Section GetSection(int SectionId)
    {
        var _section = from s in DataContext.Sections where s.SectionId == SectionId select s;

        return _section.Select(x => new PAWeb.Domain.Entities.Section()
        {
            SectionId = x.SectionId,
            Title = x.Title,
            UrlTitle = x.UrlTitle,
            NavTitle = x.NavTitle,
            AreaId = x.AreaId,
            Ordinal = x.Ordinal
        }
        ).FirstOrDefault();
    }  

This will either return the first item, or null if there are no items that match your query. In your case that won't happen unless the table is empty since you don't have a where clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文