Entity Framework classes, extending EF classes and adding custom properties, iQueryable and Get method
I'm at a loss as to how to title this, so if someone can rename this more appropriately, that would be appreciated.
I'm stuck as to how I c开发者_开发百科an go about populating custom properties of a partial entities class. Some examples
// partial class from EF base class - base class contains
// properties such as commentID, comment, userID etc
// Comments.cs
public partial class Comment {
public int UpVotes { get; set; }
public int DownVotes { get; set; }
public int CurrentVotes()
{
return UpVotes - DownVotes;
}
}
_
//CommentRepository.cs
//snip
public Comment GetItem(int id)
{
Comment c = db.Comments.SingleOrDefault(x => x.CommentID == id);
c.UpVotes = 0 //get UpVotes
c.DownVotes = 0 // get DownVotes
return c;
}
public IQueryable<Comment> GetAllCommentsByPage(int pageid)
{
}
public IQueryable<Comment> GetAllCommentsByPage(string slug)
{
}
public IQueryable<Comment> GetCommentSelection(int PageID, int Skip, int Take)
{
}
public int CountCommentByPage(int PageID)
{
}
In the old .Net 1.x days, I would have had the GetAllx methods select a list of CommentIDs, then populated a new List by using List.Add(GetItem(ID)).
In EF4, I want to do similar, but not lose out on the delayed execution of IQueryables.
In a couple of instances I have found it useful to stack these GetAllx methods in quick succession before doing a final .ToList() or similar to get the actual data.Is there a sensible way for me to do what I hope is fairly simple and eluding me? I'd hate to have to change each method to return a static List of items that can be generated through the one GetItem method.
Thanks in advance.
----- edit -----
Okay, here's the solution I have currently:
public List<Comment> IQueryable2ToList(IQueryable<Comment> c)
{
List<Comment> comments = new List<Comment>();
List<int> ids = c.Select(x => x.CommentID).ToList();
foreach (int id in ids) {
comments.Add(GetComment(id));
}
return comments;
}
Which I call by doing:
List<Comment> comments = (new CommentRepository()).IQueryable2ToList(db.FindAllCommentsByPage(slug));
Which just seems a little dirty somehow...
Well, you can eliminate the n+1 selects:
public List<Comment> IQueryable2ToList(IQueryable<Comment> comments)
{
List<Comment> comments = comments.ToList()
foreach (Comment c in comments) {
c.UpVotes = 0 //get UpVotes
c.DownVotes = 0 // get DownVotes
}
return comments;
}
However, that's not what I'd do. Instead I'd make a presentation model and project:
public class CommentPresentation {
public int CommentID { get; set; }
public string WittyInsight { get; set; }
public int UpVotes { get; set; }
public int DownVotes { get; set; }
public int CurrentVotes()
{
return UpVotes - DownVotes;
}
}
public IQueryable<CommentPresentation> ToPresentation(IQueryable<Comment> comments)
{
return from c in comments
select new CommentPresentation
{
CommentId = c.CommentId,
WittyInsight = c.WittyInsight,
UpVotes = 0,
DownVotes = 0
};
}
If you want to assign something other than a constant, you may have to go through AsEnumerable()
, so you'd want to do paging first. But this should get you started.
精彩评论