实体框架 - POCO 通用列表属性中的 LINQ 选择
我在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是否有理由简单地删除最后的
FirstOrDefault
不是这里的解决方案?我觉得我可能误会了什么。编辑:
我想我明白你想做什么。问题是您正在 select 语句中创建一个列表,而 select 语句一次只能处理一件事。它基本上是将输入类型映射到新的输出类型。
尝试这样的事情:
作为旁注,除非您由于某种原因实际上需要
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:
As a side note, unless you actually need a
List
for some reason, I would storefoo.eventList
asIEnumerable<SalesEvent>
instead. This allows you to skip theList
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 theSalesEventMatchVo
initializer, but I'm pretty sure you can chop them out. If nothing else, you shouldSelect
afterWhere
, asWhere
can reduce the work performed by all following statements.