实体框架类,扩展 EF 类并添加自定义属性、iQueryable 和 Get 方法
我不知道如何命名它,所以如果有人可以更合适地重命名它,那将不胜感激。
我不知道如何填充部分实体类的自定义属性。一些示例
// 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)
{
}
在旧的 .Net 1.x 时代,我会让 GetAllx 方法选择 CommentID 列表,然后使用 List.Add(GetItem(ID)) 填充新列表。
在 EF4 中,我想做类似的事情,但不会失去 IQueryables 的延迟执行。
在一些情况下,我发现在执行最终的 .ToList() 或类似操作以获取实际数据之前快速连续地堆叠这些 GetAllx 方法很有用。
有没有一种明智的方法可以让我做我希望相当简单但又让我困惑的事情?我不想更改每个方法以返回可以通过一个 GetItem 方法生成的静态项目列表。
提前致谢。
----- 编辑 -----
好吧,这是我目前拥有的解决方案:
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;
}
我通过这样做来调用:
List<Comment> comments = (new CommentRepository()).IQueryable2ToList(db.FindAllCommentsByPage(slug));
这看起来有点脏......
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 can 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您可以消除 n+1 个选择:
但是,这不是我会做的。相反,我会制作一个演示模型和项目:
如果您想分配常量以外的其他内容,您可能必须通过
AsEnumerable()
,因此您需要首先进行分页。但这应该让你开始。Well, you can eliminate the n+1 selects:
However, that's not what I'd do. Instead I'd make a presentation model and project:
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.