[英]How to join multiple table's data in one GridView in ASP.Net Webform on button_click
我在GridView中單擊一個按鈕即可填充所有事務數據。 但是,數據庫中的交易表不包含維表(例如,帳戶表)數據,例如交易表包含AccountID,但它不具有我必須從帳戶表中攜帶的AccountName屬性。 在DataAccess層中,方法(GetAllTransactions())確實包含AccountID上Transaction和Account表之間的聯接,但是當我嘗試選擇account.AccountName時,它不起作用。
我只需要顯示AccountID匹配的Transaction表中的幾個表,並顯示AccountName列而不是Transaction表中的AccountID。
DataAccess方法如下所示:
public static IEnumerable<Transaction>GetAllTransactions()
{
List<Transaction> allTransactions = new List<Transaction>();
using (var context = new CostReportEntities())
{
allTransactions = (from t in context.Transactions
join account in context.Accounts on t.AccountID equals account.AccountID
select t).ToList();
}
return allTransactions;
}
.aspx頁后面的代碼如下:
protected void _UIButtonSubmit_Click(object sender, EventArgs e)
{
IEnumerable<Transaction> transactions = ts.GetAllTransactions();
_UITransactionGridView.DataSource = transactions;
_UITransactionGridView.DataBind();
_UITransactionGridView.PageIndex = 0;
}
我也嘗試了以下一些方法,但是我沒有得到LINQ的主意。 以下給出錯誤。
...select new {t.*, account.AccountName}).ToList();
我需要一個方向,以便使任務能夠在一個GridView中填充來自多個表的數據。 謝謝。
由於我的交易模型沒有帳戶表的任何屬性,因此我必須創建一個ViewModel類型類,該類為每個交易返回AccountNumber的自定義數據。
我用來解決問題的代碼是這樣的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.