Linq To 实体查询
考虑以下查询:
var profilelst =
(
from i in dbContext.ProspectProfiles
where i.CreateId == currentUser
select new ProspectProfile
{
ProspectId = i.ProspectId,
Live = i.Live,
Name = i.Name,
ServiceETA = i.Opportunities.OrderByDescending(t => t.FollowUpDate)
.FirstOrDefault()
.ServiceETA.ToString(),
FollowUpDate = i.Opportunities.OrderByDescending(t => t.FollowUpDate)
.FirstOrDefault()
.FollowUpDate
}
)
.ToList();
return profilelst.OrderByDescending(c=>c.FollowUpDate)
.Skip(0).Take(endIndex)
.ToList();
在此查询中,请查看 FollowUpDate
和 ServiceType
,这两个都是我从 Opportunity 表中获取的,是否还有其他解决办法来获取这些两者..
表中的一对多关系类似于: ProspectProfile ->机会
我编写的查询是否可以,或者是否有其他可以以更简单的方式完成的解决方案。
Consider the following Query :
var profilelst =
(
from i in dbContext.ProspectProfiles
where i.CreateId == currentUser
select new ProspectProfile
{
ProspectId = i.ProspectId,
Live = i.Live,
Name = i.Name,
ServiceETA = i.Opportunities.OrderByDescending(t => t.FollowUpDate)
.FirstOrDefault()
.ServiceETA.ToString(),
FollowUpDate = i.Opportunities.OrderByDescending(t => t.FollowUpDate)
.FirstOrDefault()
.FollowUpDate
}
)
.ToList();
return profilelst.OrderByDescending(c=>c.FollowUpDate)
.Skip(0).Take(endIndex)
.ToList();
Here in this query please take a look at FollowUpDate
and ServiceType
, these both i have fetched from Opportunity table, is there any other work around to get these both..
One to Many Relationship in tables is like: ProspectProfile -> Opportunities
Whether the query i have written is ok or is there any another work around that can be done in easier way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以改进的唯一事情是通过将代码更改为以下内容来避免排序两次:
我对原始查询进行了一些更改:
Skip(0)
。Select
部分:FirstOrDefault
更改为First
,因为您无论如何都会访问返回值的属性。如果不存在机会,这将引发描述性异常。这比您所拥有的要好:在您的情况下,它将抛出 NullReferenceException 。这很糟糕,NullReferenceExceptions
总是表明程序中存在错误,而且根本没有描述性。The only thing you can improve is to avoid ordering twice by changing your code to this:
I made several changes to your original query:
Skip(0)
.Select
part:FirstOrDefault
toFirst
, because you are accessing the properties of the return value anyway. This will throw a descriptive exception if no opportunity exists. That's better than what you had: In your case it would throw aNullReferenceException
. That's bad,NullReferenceExceptions
always indicate a bug in your program and are not descriptive at all.您的查询中存在不少问题:
您无法投影到实体中(
选择新的 ProspectProfile
)。 LINQ to Entities 仅支持投影到匿名类型 (select new
) 或不属于实体数据模型的其他类型 (select new MySpecialType
)LINQ to Entities (
ServiceETA.ToString()
) 不支持数字或DateTime
类型的 ToString()FirstOrDefault().ServiceETA
(如果Opportunities
集合为空并且ServiceETA
是不可为 null 的值类型(例如DateTime
),因为 EF 无法将任何值具体化到此类变量中。在第一个查询后使用
.ToList()
将在数据库中执行查询并加载完整结果。您稍后的Take
发生在完整列表的内存中,而不是数据库中。 (您可以有效地将整个结果列表从数据库加载到内存中,然后丢弃除第一个之外的所有对象Take
en。您可以尝试以下操作:
如果您需要实体
ProspectProfile
的列表,这将为您提供匿名对象的列表。您必须复制此查询后的值。请注意,如果ProspectProfile
没有Opportunities
,结果中的LastOpportunity
可能为null
。代码>.There are quite a few problems in your query:
You cannot project into an entity (
select new ProspectProfile
). LINQ to Entities only supports projections into anonymous types (select new
) or other types which are not part of your entity data model (select new MySpecialType
)ToString()
for a numeric orDateTime
type is not supported in LINQ to Entities (ServiceETA.ToString()
)FirstOrDefault().ServiceETA
(orFollowUpdate
) will throw an exception if theOpportunities
collection is empty andServiceETA
is a non-nullable value type (such asDateTime
) because EF cannot materialize any value into such a variable.Using
.ToList()
after your first query will execute the query in the database and load the full result. Your laterTake
happens in memory on the full list, not in the database. (You effectively load the whole result list from the database into memory and then throw away all objects except the first you haveTake
en.To resolve all four problems you can try the following:
This will give you a list of anonymous objects. If you need the result in a list of your entity
ProspectProfile
you must copy the values after this query. Note thatLastOpportunity
can benull
in the result if aProspectProfile
has noOpportunities
.