これが私が使った方法です…。
まず、データベース内のテーブルを表すクラスを作成します:-
public class Contact
{
public int ContactID { get; set; }
public string Surname { get; set; }
public string Forename { get; set; }
public string MobileNumber { get; set; }
public string EmailAddress { get; set; }
public string Information { get; set; }
}
次に、このデータをIEnumerableリストにロードします:-
public List<Contact> GetContacts()
{
DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Contacts]", Connection);
Adapter.SelectCommand = cmd;
Connection.Open();
Adapter.SelectCommand.ExecuteNonQuery();
Adapter.Fill(dt);
Connection.Close();
var Contacts = (from row in dt.AsEnumerable()
select new Contact
{
ContactID = row.Field<int>("ContactID"),
Surname = row.Field<string>("Surname"),
Forename = row.Field<string>("Forename"),
MobileNumber = row.Field<string>("MobileNumber"),
EmailAddress = row.Field<string>("EmailAddress"),
Information = row.Field<string>("Information")
}).ToList();
return Contacts;
}
私のアプリケーションでは、このオブジェクトのインスタンスを作成します:-
public List<Contact> contactData;
contactData = dc.GetContacts();
LINQを使用してデータを操作できるようになりました:-
var Query = ConactData.Where(item=> item.ContactID == 10)
.Select(item=> item.Surname).toString();
LINQを使用してデータをクエリし、リスト、文字列などとして保存できます。
お役に立てれば。