实体框架类,扩展 EF 类并添加自定义属性、iQueryable 和 Get 方法

发布于 2024-10-17 20:31:41 字数 1807 浏览 0 评论 0原文

我不知道如何命名它,所以如果有人可以更合适地重命名它,那将不胜感激。

我不知道如何填充部分实体类的自定义属性。一些示例

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2024-10-24 20:31:41

好吧,您可以消除 n+1 个选择:

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;
}

但是,这不是我会做的。相反,我会制作一个演示模型和项目:

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
           };
}

如果您想分配常量以外的其他内容,您可能必须通过 AsEnumerable(),因此您需要首先进行分页。但这应该让你开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文