使 Linq Entity 对象适应 Domain 对象

发布于 2024-07-11 11:07:26 字数 1804 浏览 0 评论 0原文

我有以下代码,可将 linq 实体适应我的域对象:

return from g in DBContext.Gigs
               select new DO.Gig
               {
                   ID = g.ID,
                   Name = g.Name,
                   Description  = g.Description,
                   StartDate    = g.Date,
                   EndDate      = g.EndDate,
                   IsDeleted    = g.IsDeleted,
                   Created      = g.Created,
                   TicketPrice  = g.TicketPrice

               };

这非常有效。

不过,我现在想要填充一个域对象 Venue 对象,并将其添加到同一语句中的演出中。 这是我的尝试...

return from g in DBContext.Gigs
               join venue in DBContext.Venues on g.VenueID equals venue.ID
               select new DO.Gig
               {
                   ID = g.ID,
                   Name = g.Name,
                   Description  = g.Description,
                   StartDate    = g.Date,
                   EndDate      = g.EndDate,
                   IsDeleted    = g.IsDeleted,
                   Created      = g.Created,
                   TicketPrice  = g.TicketPrice,
                   Venue        =     from v in DBContext.Venues
                                        where v.ID == g.VenueID
                                        select new DO.Venue
                                        {
                                            ID           = v.ID,
                                            Name         = v.Name,
                                            Address      = v.Address,
                                            Telephone    = v.Telephone,
                                            URL          = v.Website 
                                        }

               };

但是这不能编译!

是否可以使用“选择新”方法来调整子对象?

我做错了什么?

I have the following code which adapts linq entities to my Domain objects:

return from g in DBContext.Gigs
               select new DO.Gig
               {
                   ID = g.ID,
                   Name = g.Name,
                   Description  = g.Description,
                   StartDate    = g.Date,
                   EndDate      = g.EndDate,
                   IsDeleted    = g.IsDeleted,
                   Created      = g.Created,
                   TicketPrice  = g.TicketPrice

               };

This works very nicely.

However I now want to populate a domain object Venue object and add it to the gig in the same statement. Heres my attempt....

return from g in DBContext.Gigs
               join venue in DBContext.Venues on g.VenueID equals venue.ID
               select new DO.Gig
               {
                   ID = g.ID,
                   Name = g.Name,
                   Description  = g.Description,
                   StartDate    = g.Date,
                   EndDate      = g.EndDate,
                   IsDeleted    = g.IsDeleted,
                   Created      = g.Created,
                   TicketPrice  = g.TicketPrice,
                   Venue        =     from v in DBContext.Venues
                                        where v.ID == g.VenueID
                                        select new DO.Venue
                                        {
                                            ID           = v.ID,
                                            Name         = v.Name,
                                            Address      = v.Address,
                                            Telephone    = v.Telephone,
                                            URL          = v.Website 
                                        }

               };

However this doesnt compile!!!

Is it possible to adapt children objects using the "select new" approach?

What am I doing so very very wrong?

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

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

发布评论

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

评论(2

情绪少女 2024-07-18 11:07:26

您的内部 LINQ 查询返回多个对象,而不仅仅是一个。 你想用这样的调用来包装它:

Venue = (from v in DBContext.Venues
         where v.ID == g.VenueID
         select new DO.Venue
         {
             ID           = v.ID,
             Name         = v.Name,
             Address      = v.Address,
             Telephone    = v.Telephone,
             URL          = v.Website 
         }).SingleOrDefault()

你选择 Single() vs. SingleOrDefault() vs. First() vs. FirstOrDefault() 取决于它是什么类型的查询,但我猜你想要其中一个前两个。 (如果查询没有数据,“OrDefault”变体将返回 null;其他变体则抛出异常。)

我也同意 Mike 的观点,即如果涉及单一关系,联接可能更符合您的要求。

Your inner LINQ query returns several objects, not just one. You want to wrap it with a call like:

Venue = (from v in DBContext.Venues
         where v.ID == g.VenueID
         select new DO.Venue
         {
             ID           = v.ID,
             Name         = v.Name,
             Address      = v.Address,
             Telephone    = v.Telephone,
             URL          = v.Website 
         }).SingleOrDefault()

Your choice of Single() vs. SingleOrDefault() vs. First() vs. FirstOrDefault() depends on what kind of query it is, but I'm guessing you want one of the first two. (The "OrDefault" variants return null if the query has no data; the others throw.)

I also agree with Mike that a join might be more in line with what you wanted, if there's a singular relationship involved.

萌化 2024-07-18 11:07:26

为什么要进行联接和子选择? 您可以仅使用加入的结果来创建新场地。 请注意,如果演出和场地之间没有一对一的关系,您可能会遇到麻烦。

尝试这个:

return from g in DBContext.Gigs 
    join venue in DBContext.Venues on g.VenueID equals venue.ID 
    select new DO.Gig { ID = g.ID, Name = g.Name, Description = g.Description,
        StartDate = g.Date, EndDate = g.EndDate, IsDeleted = g.IsDeleted, 
        Created = g.Created, TicketPrice = g.TicketPrice, 
        Venue = new DO.Venue { ID = venue.ID, Name = venue.Name, 
            Address = venue.Address, Telephone = v.Telephone, 
            URL = v.Website }

Why are you doing a join and a sub select? You can just use the results of your join in the creation of a new Venue. Be aware that if there is not a one to one relationship between gigs and venues you could run into trouble.

Try this:

return from g in DBContext.Gigs 
    join venue in DBContext.Venues on g.VenueID equals venue.ID 
    select new DO.Gig { ID = g.ID, Name = g.Name, Description = g.Description,
        StartDate = g.Date, EndDate = g.EndDate, IsDeleted = g.IsDeleted, 
        Created = g.Created, TicketPrice = g.TicketPrice, 
        Venue = new DO.Venue { ID = venue.ID, Name = venue.Name, 
            Address = venue.Address, Telephone = v.Telephone, 
            URL = v.Website }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文