实体框架4.0 - 返回模型对象列表+每个对象的子代数 [英] Entity Framework 4.0 - Returning List of Model objects + Count of Children Per Object
问题描述
我想能够动态地分页构建计数属性也。我认为最可扩展的方法是将计数存储为单独的数据库属性,然后简单地查询计数属性。但是对于我正在处理的小行数的情况,我宁愿动态地进行计数。
在这样的模式中:
表:类
表:教授
表:与会者
表:ClassComment
我想以List的形式返回Class对象的列表,但是我也希望在单个查询(LINQ首选)中确定参加者和类注释的计数,并设置在两个Class属性中名为AttendeeCount和ClassCommentCount。
我有这个目的:
var query = from u在上下文中。
orderby tl.Name
select u;
列表<类> topic =((ObjectQuery&Class>)查询)
.Include(ClassComments)
.Skip(startRecord).Take(recordsToReturn).ToList();
任何仍然允许使用.Include()和分页的建议或替代查询方法将是非常感谢,为了生成单个数据库查询,如果可能的话。感谢您的任何建议!
尝试这样:
public class ClassViewModel {
public Class Class {get;组; }
public int AttendeeCount {get;组; }
public int ClassCommentCount {get;组; }
}
var viewModel = context.Classes.Select(clas =>
new ClassViewModel {
Class = clas,
AttendeeCount = clas.ClassAttendes.Count,
ClassCommentCount = clas.ClassComments.Count}
).OrderBy(model => model.ClassCommentCount).Skip(startRecord).Take(recordsToReturn).ToList() ;
您不需要包含评论来计数。
I'm running into a common need in my project to return collections of my model objects, plus a count of certain types of children within each, but I don't know if it is possible or how to model a "TotalCount" property in a Model class and populate it as part of on single Entity Framework query, preferably using LINQ queries. Is it possible to do this whilst being able to use the Entity Framework .Include("Object") and .Skip() and .Take()? I'm new to the Entity Framework so I may be missing tons of obvious stuff that can allow this...
I would like to be able to paginate on the dynamically constructed count properties as well. I'm thinking that the most scalable approach would be to store the counts as separate database properties and then simply query the count properties. But for cases where there are small row counts that I'm dealing with, I'd rather do the counts dynamically.
In a model like this:
Table: Class
Table: Professor
Table: Attendee
Table: ClassComment
I'd like to return a list of Class objects in the form of List, but I would also like the counts of Attendees and Class comments to be determined in a single query (LINQ preferred) and set in two Class properties called AttendeeCount and ClassCommentCount.
I have this thus far:
var query = from u in context.Classes
orderby tl.Name
select u;
List<Class> topics = ((ObjectQuery<Class>)query)
.Include("ClassComments")
.Skip(startRecord).Take(recordsToReturn).ToList();
Any suggestions or alternative query approaches that can still allow the use of .Include() and pagination would be much much appreciated, in order to produce a single database query, if at all possible. Thank you for any suggestions!
Try this:
public class ClassViewModel {
public Class Class { get; set; }
public int AttendeeCount { get; set; }
public int ClassCommentCount { get; set; }
}
var viewModel = context.Classes.Select(clas =>
new ClassViewModel {
Class = clas,
AttendeeCount = clas.ClassAttendes.Count,
ClassCommentCount = clas.ClassComments.Count}
).OrderBy(model => model.ClassCommentCount).Skip(startRecord).Take(recordsToReturn).ToList();
You don't have to include comments to get count.
这篇关于实体框架4.0 - 返回模型对象列表+每个对象的子代数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!