0
使用實體框架如何選擇要顯示在文本框中的任何字符串。在過去,我會做後續的使用myReader:選擇要在EF中的文本框中顯示的任何字符串
while (myReader.Read())
{
string sName = myReader.GetString(1);
txtName.Text = sName;
}
使用實體框架如何選擇要顯示在文本框中的任何字符串。在過去,我會做後續的使用myReader:選擇要在EF中的文本框中顯示的任何字符串
while (myReader.Read())
{
string sName = myReader.GetString(1);
txtName.Text = sName;
}
您必須加載實體(=表),你的願望,比實體的屬性分配給該文本框。
喜歡的東西:
using (var myContext = new EntityContext())
{
myContext.TableName.Load(); //this will load all rows from the table
var list = myContext.TableName.Local; //.Local is a list of the entities which gets populated when you cann the Load() method
//now you can either enumarte your list like so
foreach (TableName item in list)
{
string sName = item.PropertyName; //PropertyName is the property you want to display, this would be like using not an index in myReader.GetString(1) but the columnname myReader.GetString("ColumnName")
txtName.Text = sName;
}
//or if you want to get a certain item out of your list you can use LINQ/MethodChains
//MethodChain
var item = list.First(x => x.PropertyName == "HelloWorld!");
string sName = item.PropertyName;
txtName.Text = sName;
//LINQ
var item = (from x in list
where x.PropertyName == "HelloWorld!"
select x).First();
string sName = item.PropertyName;
txtName.Text = sName;
//the methodchain and linq is a "new" technology that simplifies this code (which does the same)
TableName item = null;
foreach (TableName tempItem in list)
{
if (tempItem.PropertyName == "HelloWorld!")
{
item = tempItem;
break;
}
}
string sName = item.PropertyName;
txtName.Text = sName;
}