我想创建一个通用方法,根据给定的参数从 Linq 获取数据,例如:table、field1 和 field2。
将使用一种方法代替这两种特定方法,例如:
public void DistributeCB(ComboBox cb)
{
BooksDBDataContext db = new BooksDBDataContext();
Type T = db.GetType();
//Authors table
var data =db.Authors.Select(author => new
{
Id = author.AuthorId, //field 1
Value = author.AuthorName //field 2
});
cb.ValueMember = "Id";
cb.DisplayMember = "Value";
cb.DataSource = data;
}
public void DistributeCB2(ComboBox cb)
{
BooksDBDataContext db = new BooksDBDataContext();
//Publishers table
var data = db.Publishers.Select(publisher => new
{
Id = publisher.PublishingId, //field 1
Value = publisher.PublishingName //field 2
});
cb.ValueMember = "Id";
cb.DisplayMember = "Value";
cb.DataSource = data;
}