我一般从 asp.net mvc3 和 mvc 开始,同时我也在学习实体框架。
我正在构建的应用程序允许用户为其他用户创建账单或付款,我不确定在编写查询时如何为账单和帐户类创建关系。我遇到的问题是我将 accountId 存储在数据库中,但是我想从 toAccountId 和 fromAccountId 中显示用户的名字和姓氏。
课程
public class Bill
{
public int BillId { get; set; }
public string BillName { get; set; }
public string BillDescription { get; set; }
public decimal Price { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateDue { get; set; }
public int FromAccountId { get; set; }
public int ToAccountId { get; set; }
}
public class Account
{
public int AccountId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobilePhone { get; set; }
public string BankAccountNumber { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime LastLogin { get; set; }
}
public class HomeViewModel
{
public IEnumerable<Bill> Bills { get; set; }
}
在控制器中
private IBillRepository repository;
public ViewResult Home()
{
int userID = 1;
HomeViewModel viewModel = new HomeViewModel
{
Bills = repository.Bills
.OrderByDescending(b => b.DateCreated)
.Where(b => b.FromAccountId.Equals(userID))
.Take(amountOfBillsOnPageOnLoad)
};
return View(viewModel);
}