关于我在评论中提到的 ORM。这是一个关于如何为实体框架定义一个类(代码优先)的示例,您可以很容易地从数据库表中进行逆向工程。 Entity Framework 会为您做到这一点。
下面是我之前在此论坛上发布的另一个问题的示例。您可以看到使用 LINQ 从数据库中的表中检索数据是多么容易,您还可以做更多事情。
namespace ToPagedList
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Course")]
public partial class Course
{
public int CourseId { get; set; }
public int? CourseGroupId { get; set; }
[StringLength(50)]
public string CourseCode { get; set; }
[Required]
[StringLength(100)]
public string Title { get; set; }
[StringLength(1000)]
public string ShortDescription { get; set; }
[Column(TypeName = "ntext")]
[Required]
public string Contents { get; set; }
[Column(TypeName = "ntext")]
public string MailTextApplied { get; set; }
[Column(TypeName = "ntext")]
public string MailTextAccepted { get; set; }
[Column(TypeName = "ntext")]
public string MailTextRejected { get; set; }
[Column(TypeName = "ntext")]
public string MailTextDeleted { get; set; }
[StringLength(1000)]
public string ExternalRegistrationUrl { get; set; }
public bool Visible { get; set; }
[StringLength(1000)]
public string ExternalCourseUrl { get; set; }
public string Reviews { get; set; }
[StringLength(50)]
public string School { get; set; }
[Column(TypeName = "ntext")]
public string MailTextCertified { get; set; }
[Column(TypeName = "ntext")]
public string MailTextNoShow { get; set; }
[Column(TypeName = "ntext")]
public string MailTextPassed { get; set; }
[Column(TypeName = "ntext")]
public string MailTextPartlyCompleted { get; set; }
[Column(TypeName = "ntext")]
public string MailTextWaitingList { get; set; }
[Column(TypeName = "ntext")]
public string MailTextWithdrawn { get; set; }
public bool ShowFromStartDate { get; set; }
}
}
这是检索数据的代码:
using System;
using System.Diagnostics;
namespace ToPagedList
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var list1 = Repository.GetCourses1();
sw.Stop();
Console.WriteLine($"Getting list 1 took {sw.ElapsedMilliseconds} milliseconds");
sw.Reset();
sw.Start();
var list2 = Repository.GetCourses2();
sw.Stop();
Console.WriteLine($"Getting list 2 took {sw.ElapsedMilliseconds} milliseconds");
Console.ReadKey();
}
}
}
还有 Repository 类:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ToPagedList
{
public class Repository
{
public static List<DocumentsModel> GetCourses1(string school = null, string code = null, string title = null, int page = 0, int count = 15)
{
var courses = new DocumentModelEntities().Course;
return courses.Where(course => string.IsNullOrEmpty(code) || course.CourseCode.Contains(code))
.Where(course => String.IsNullOrEmpty(title) || course.Title.Contains(title))
.Where(w => String.IsNullOrEmpty(school) || w.School == school)
// From here your table is read from the DB using the where clauses
.Select(s => new DocumentsModel
{
Code = code.Trim(),
Title = s.Title
})
.OrderBy(o => o.Code)
.Skip(page * count)
.Take(count)
.ToList();
}
public static List<DocumentsModel> GetCourses2(string school = null, string code = null, string title = null, int page = 0, int count = 15)
{
var courses = new DocumentModelEntities().Course;
return courses.Where(course => string.IsNullOrEmpty(code) || course.CourseCode.Contains(code))
.Where(course => String.IsNullOrEmpty(title) || course.Title.Contains(title))
.Where(w => String.IsNullOrEmpty(school) || w.School == school)
.OrderBy(course => course.CourseCode)
.Skip(page * count)
.Take(count)
// From here your table is read from the DB using the where clauses, order by, skip and take
.Select(s => new DocumentsModel
{
Code = code.Trim(),
Title = s.Title
})
.ToList();
}
}
}