Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实体框架-POCO通用列表属性中的LINQ选择_Linq_Entity Framework_Generics_Collections - Fatal编程技术网

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

实体框架-POCO通用列表属性中的LINQ选择,linq,entity-framework,generics,collections,Linq,Entity Framework,Generics,Collections,我在从EF上下文设置POCO对象的通用列表属性时遇到一些问题。例如,我有一个非常简单的对象,它包含以下内容: public class foo { public string fullName; public Entity entity; public List<SalesEvent> eventList; } 公共类foo { 公共字符串全名; 公共实体; 公共列表事件列表; } 从中填充此对象的代码如下所示: .Select(x =>

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

 public class foo
 {
    public string fullName;
    public Entity entity;
    public List<SalesEvent> eventList;
 }
公共类foo
{
公共字符串全名;
公共实体;
公共列表事件列表;
}
从中填充此对象的代码如下所示:

  .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();
。选择(x=>newfoo()
{
全名=x.vchFirstName+“”+x.vchLastName,
实体=新的EntityVo()
{
地址1=x.vchAddress1,
entityId=x.iEntityId,
emailAddress=x.vchEmailAddress,
firstName=x.vchFirstName,
lastName=x.vchLastName,
城市=x.vchCity,
状态=x.chState,
工作编号=x.VCH工作编号,
mobileNumber=x.vchMobileNumber,
siteId=x.iSiteId
}
eventList=\u context.Events
.其中(e=>e.iEntityId==x.iEntityId
&&e.iStatusId>=事件状态分钟
&&e.iStatusId新列表
{
新的SalesEventMatchVo()
{
vehicleName=\u context.Quotes.Select(q=>q).Where(q=>q.iEventId==e.iEventId.FirstOrDefault().vchMake++++\u context.Quotes.Select(q=>q.iEventId==e.iEventId.FirstOrDefault().vchModel,
eventId=e.iEventId,
销售人员=e.chAssignedTo,
eventStatusDesc=\u context.RefDefinitions.Select(r=>r).Where(r=>r.ipParameterId==e.iStatusId).FirstOrDefault().vchParameterDesc,
eventStatusId=(int)e.iStatusId,
eventSourceDesc=\u context.RefDefinitions.Select(r=>r).Where(r=>r.ipParameterId==e.iSourceId).FirstOrDefault().vchParameterDesc,
createDate=e.dtInsertDate
}
}).FirstOrDefault()
}).ToArray();

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

是否有理由简单地删除最后的
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
eventList=\u context.Events.Where(e=>e.iEntityId==x.iEntityId&&//FILTER Events
e、 iStatusId>=事件状态分钟&&
e、 iStatusId new SalesEventMatchVo()//映射到SALESEVENT
{
vehicleName=\u context.Quotes.Select(q=>q).Where(q=>q.iEventId==e.iEventId.FirstOrDefault().vchMake++++\u context.Quotes.Select(q=>q.iEventId==e.iEventId.FirstOrDefault().vchModel,
eventId=e.iEventId,
销售人员=e.chAssignedTo,
eventStatusDesc=\u context.RefDefinitions.Select(r=>r).Where(r=>r.ipParameterId==e.iStatusId).FirstOrDefault().vchParameterDesc,
eventStatusId=(int)e.iStatusId,
eventSourceDesc=\u context.RefDefinitions.Select(r=>r).Where(r=>r.ipParameterId==e.iSourceId).FirstOrDefault().vchParameterDesc,
createDate=e.dtInsertDate
})
.ToList()//转换为列表
作为旁注,除非您出于某种原因确实需要
列表
,否则我会将
foo.eventList
存储为
IEnumerable
。这允许您跳过最后的
列表
转换,并且在某些情况下启用了延迟和/或部分执行等巧妙的技巧

另外,我不确定您的
.Select(q=>q)
语句在
SalesEventMatchVo
初始值设定项的几行中有什么意义,但我很确定您可以将它们删掉。如果没有其他内容,您应该在
Where
之后选择
,因为
Where
可以减少以下所有语句执行的工作量