C# 需要关于访问数据库的建议吗
假设我有一个具有属性及其方法的注释类 像 及C# 需要关于访问数据库的建议吗,c#,.net,database,list,C#,.net,Database,List,假设我有一个具有属性及其方法的注释类 像 及 publicstaticlist GetComments() 公共静态列表GetCommentsByAuthor(作者id) 现在,我通常要做的是为每个 以上方法。 也就是说,现在我看到了BlogEngine.NET代码,他在这篇文章中写道 方式; 他为GetComments()方法编写了数据库逻辑,并提取了 对于所有剩余的方法,甚至对于GetComment(Guid id)by 使用类似 //FindAll is a method in L
publicstaticlist GetComments()
公共静态列表GetCommentsByAuthor(作者id)
现在,我通常要做的是为每个
以上方法。
也就是说,现在我看到了BlogEngine.NET代码,他在这篇文章中写道
方式;
他为GetComments()方法编写了数据库逻辑,并提取了
对于所有剩余的方法,甚至对于GetComment(Guid id)by
使用类似
//FindAll is a method in List<T> class
FindAll(delegate(Comment comment)
{
return comment.Author.Equals(author ,
StringComparison.OrdinalIgnoreCase);
}
);
//FindAll是List类中的一个方法
FindAll(代表(评论)
{
返回comment.Author.Equals(Author,
对照组(例);
}
);
我的问题是,用这种方式而不是用传统的方法做这件事是一个好主意吗
第一种方法?
这种方式有什么优点和缺点,有什么建议和建议吗
资源是最受欢迎的。
短暂性脑缺血发作
Srinivas在可能的情况下重用代码而不是重写代码是个好主意,但这不是最好的方法。它需要从数据库中提取所有行,然后在客户机上过滤它们。如果使用Linq,您可以遵循相同的模式重用查询,但是过滤将在数据库中而不是在客户机上进行,从而提高性能
public IQueryable<Comment> GetCommentsByAuthor(Author author) {
return GetComments().Where(comment => comment.Author.Id == author.Id);
}
public IQueryable GetCommentsByAuthor(作者){
返回GetComments().Where(comment=>comment.Author.Id==Author.Id);
}
//FindAll is a method in List<T> class
FindAll(delegate(Comment comment)
{
return comment.Author.Equals(author ,
StringComparison.OrdinalIgnoreCase);
}
);
public IQueryable<Comment> GetCommentsByAuthor(Author author) {
return GetComments().Where(comment => comment.Author.Id == author.Id);
}