Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要关于访问数据库的建议吗_C#_.net_Database_List - Fatal编程技术网

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