我正在从数据库的两个表中获取数据。
表 1 - 成员:
public List<Member> ListMembers()
{
List<Member> Members = new List<Member>();
string SELECT = "SELECT * FROM Members ORDER BY Id";
using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand(SELECT, sqlConnection))
{
var sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
var member = new Member
{
Id = Convert.ToInt32(sqlReader["Id"]),
Name = sqlReader["Name"].ToString(),
Surname = sqlReader["Surname"].ToString(),
EntryDate = Convert.ToDateTime(sqlReader["EntryDate"])
};
if (member.EntryDate < DateTime.Today)
{
member.Status = Status.Unpaid.ToString();
}
else
{
member.Status = Status.Paid.ToString();
}
Members.Add(member);
}
}
}
return Members;
}
表 2 - 付款:
public List<Payment> ListPayments(Payment entity)
{
List<Payment> Payments = new List<Payment>();
string SELECT = "SELECT * FROM Payments WHERE Id = @Id";
using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand(SELECT, sqlConnection))
{
sqlCommand.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;
var sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
var payment = new Payment
{
Id = Convert.ToInt32(sqlReader["Id"]),
Amount = Convert.ToDecimal(sqlReader["Amount"]),
StartDay = Convert.ToDateTime(sqlReader["StartDay"]),
EndDay = Convert.ToDateTime(sqlReader["EndDay"])
};
Payments.Add(payment);
}
}
}
return Payments;
}
付款的 ID 等于特定会员的 ID。(如果会员 ID 为 100,则他/她的所有付款都将具有相同的 ID 100)。
现在......在ListMembers()中,我根据他们的 EntryDate 给状态“已付款”和“未付款”,但我想根据他们的 LAST Payment 给他们状态。所以,而不是:
if (member.EntryDate < DateTime.Today)
{
member.Status = Status.Unpaid.ToString();
}
它应该是 :
if (lastPayment < DateTime.Today) // lastPayment is just a word I'm using, It's not declared anywhere, I don't know how to get that value.
{
member.Status = Status.Unpaid.ToString();
}
我真的迷路了,有人可以帮助我吗?我不知道该怎么办。