实体框架 - POCO 通用列表属性中的 LINQ 选择

发布于 2024-11-06 19:29:34 字数 2696 浏览 1 评论 0原文

我在从 EF 上下文设置 POCO 对象的通用列表属性时遇到一些问题。例如,我有一个非常简单的对象,其中包含以下内容:

 public class foo
 {
    public string fullName;
    public Entity entity;
    public List<SalesEvent> eventList;
 }

用于填充该对象的代码看起来像这样:

  .Select(x => new foo()
                {
                    fullName = x.vchFirstName + " " + x.vchLastName,
                    entity = new EntityVo()
                    {
                        address1 = x.vchAddress1,
                        entityId = x.iEntityId,
                        emailAddress = x.vchEmailAddress,
                        firstName = x.vchFirstName,
                        lastName = x.vchLastName,
                        city = x.vchCity,
                        state = x.chState,
                        workNumber = x.vchWorkNumber,
                        mobileNumber = x.vchMobileNumber,
                        siteId = x.iSiteId

                    }
                    eventList = _context.Events
                              .Where(e => e.iEntityId == x.iEntityId
                                        && e.iStatusId >= eventStatusMin
                                        && e.iStatusId <= eventStatusMax)
                              .Select(e => new List<SalesEventMatchVo>
                                           {
                                               new SalesEventMatchVo()
                                                   {
                                                     vehicleName = _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchMake + " " + _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchModel,
                                                     eventId =  e.iEventId,
                                                     salesPerson = e.chAssignedTo,
                                                     eventStatusDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iStatusId).FirstOrDefault().vchParameterDesc,
                                                     eventStatusId =(int)e.iStatusId,
                                                     eventSourceDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iSourceId).FirstOrDefault().vchParameterDesc,
                                                    createDate = e.dtInsertDate

                                                   }
                                           }).FirstOrDefault()
                }).ToArray();

我遇到的问题是我无法使用所有事件填充 eventList 属性,它只是获取第一条记录(查看代码是有意义的)。我似乎无法弄清楚如何填充整个列表。

I'm having some issues setting a generic list property of a POCO object when from an EF context. For instance I have a very simple object that contains the following:

 public class foo
 {
    public string fullName;
    public Entity entity;
    public List<SalesEvent> eventList;
 }

My code to populate this object from looks something like this:

  .Select(x => new foo()
                {
                    fullName = x.vchFirstName + " " + x.vchLastName,
                    entity = new EntityVo()
                    {
                        address1 = x.vchAddress1,
                        entityId = x.iEntityId,
                        emailAddress = x.vchEmailAddress,
                        firstName = x.vchFirstName,
                        lastName = x.vchLastName,
                        city = x.vchCity,
                        state = x.chState,
                        workNumber = x.vchWorkNumber,
                        mobileNumber = x.vchMobileNumber,
                        siteId = x.iSiteId

                    }
                    eventList = _context.Events
                              .Where(e => e.iEntityId == x.iEntityId
                                        && e.iStatusId >= eventStatusMin
                                        && e.iStatusId <= eventStatusMax)
                              .Select(e => new List<SalesEventMatchVo>
                                           {
                                               new SalesEventMatchVo()
                                                   {
                                                     vehicleName = _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchMake + " " + _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchModel,
                                                     eventId =  e.iEventId,
                                                     salesPerson = e.chAssignedTo,
                                                     eventStatusDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iStatusId).FirstOrDefault().vchParameterDesc,
                                                     eventStatusId =(int)e.iStatusId,
                                                     eventSourceDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iSourceId).FirstOrDefault().vchParameterDesc,
                                                    createDate = e.dtInsertDate

                                                   }
                                           }).FirstOrDefault()
                }).ToArray();

This issue I'm having is that I'm unable to populate the eventList property with all of the events, it's only grabbing the first record(which makes sense looking at the code). I just cant seem to figure out to populate a the entire list.

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

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

发布评论

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

评论(1

香橙ぽ 2024-11-13 19:29:34

是否有理由简单地删除最后的 FirstOrDefault 不是这里的解决方案?我觉得我可能误会了什么。

编辑:

我想我明白你想做什么。问题是您正在 select 语句中创建一个列表,而 select 语句一次只能处理一件事。它基本上是将输入类型映射到新的输出类型。

尝试这样的事情:

eventList = _context.Events.Where(e => e.iEntityId == x.iEntityId &&     //FILTER EVENTS
                                       e.iStatusId >= eventStatusMin &&
                                       e.iStatusId <= eventStatusMax)
                           .Select(e => new SalesEventMatchVo()          //MAP TO SALESEVENT
                                        {
                                             vehicleName = _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchMake + " " + _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchModel,
                                             eventId =  e.iEventId,
                                             salesPerson = e.chAssignedTo,
                                             eventStatusDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iStatusId).FirstOrDefault().vchParameterDesc,
                                             eventStatusId =(int)e.iStatusId,
                                             eventSourceDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iSourceId).FirstOrDefault().vchParameterDesc,
                                             createDate = e.dtInsertDate
                                         })
                           .ToList() //CONVERT TO LIST

作为旁注,除非您由于某种原因实际上需要 List ,否则我会将 foo.eventList 存储为 IEnumerable< /代码> 相反。这允许您在最后跳过 List 转换,并且在某些情况下可以启用延迟和/或部分执行等巧妙技巧。

另外,我不确定 SalesEventMatchVo 初始化程序的几行中的 .Select(q=>q) 语句的意义是什么,但我很高兴当然你可以把它们砍掉。如果不出意外,您应该在 Where 之后选择 Select,因为 Where 可以减少所有后续语句所执行的工作。

Is there a reason simply removing the FirstOrDefault at the end isn't the solution here? I feel like I might be misunderstanding something.

EDIT:

I think I see what you are trying to do. The issue is that you are creating a list in the select statement, when the select statement works only over one thing at a time. It is basically mapping an input type to a new output type.

Try something like this instead:

eventList = _context.Events.Where(e => e.iEntityId == x.iEntityId &&     //FILTER EVENTS
                                       e.iStatusId >= eventStatusMin &&
                                       e.iStatusId <= eventStatusMax)
                           .Select(e => new SalesEventMatchVo()          //MAP TO SALESEVENT
                                        {
                                             vehicleName = _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchMake + " " + _context.Quotes.Select(q=>q).Where(q=>q.iEventId == e.iEventId).FirstOrDefault().vchModel,
                                             eventId =  e.iEventId,
                                             salesPerson = e.chAssignedTo,
                                             eventStatusDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iStatusId).FirstOrDefault().vchParameterDesc,
                                             eventStatusId =(int)e.iStatusId,
                                             eventSourceDesc=_context.RefDefinitions.Select(r=>r).Where(r=>r.iParameterId==e.iSourceId).FirstOrDefault().vchParameterDesc,
                                             createDate = e.dtInsertDate
                                         })
                           .ToList() //CONVERT TO LIST

As a side note, unless you actually need a List for some reason, I would store foo.eventList as IEnumerable<SalesEvent> instead. This allows you to skip the List conversion at the end, and in some scenarios enables neat tricks like delayed and/or partial execution.

Also, I'm not sure what the point of your .Select(q=>q) statements are in several lines of the SalesEventMatchVo initializer, but I'm pretty sure you can chop them out. If nothing else, you should Select after Where, as Where can reduce the work performed by all following statements.

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