0

我正在从数据库的两个表中获取数据。

表 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();
}

我真的迷路了,有人可以帮助我吗?我不知道该怎么办。

4

2 回答 2

0

获取会员付款:

public List<Payment> GetMemberPaymnets(int memberId)
    {
        List<Payment> Payments = new List<Payment>();

        string SELECT = "SELECT ID,AMOUNT,STARTDAY,ENDDAY FROM Payments WHERE Id = @Id order by endday desc";

        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;
    }

代替

if (member.EntryDate < DateTime.Today)
{
     member.Status = Status.Unpaid.ToString();
}

你可以使用这样的东西

 foreach(Member memeber in memeberList)
 {
        List<Payment> memberPayments=GetMemberPayments(member.Id);
        //also you need to check that memberPayments.Count>0
        if(memberPayments[0].EndDay<DateTime.Today)
        {
           member.Status = Status.Unpaid.ToString();
        }
 }

但是,如果您不需要对会员付款进行一些操作,您可以创建数据库方法,该方法将通过会员 ID 返回会员付款状态。

于 2013-06-19T09:21:42.470 回答
0

您可以尝试通过查询获取最新的付款日期Member。代替

string SELECT = "SELECT * FROM Members ORDER BY Id";

尝试

string SELECT = "SELECT m.Id, m.Name, 
m.Surname, m.EntryDate, MAX(p.EndDay) AS EndDay FROM Members m
JOIN Payments p ON p.Id = m.Id
ORDER BY m.Id";

然后替换这个:

if (member.EntryDate < DateTime.Today)
{
     member.Status = Status.Unpaid.ToString();
}

有了这个:

var endDate = Convert.ToDateTime(sqlReader["EndDay"])
if (endDate < DateTime.Today)
{
    member.Status = Status.Unpaid.ToString();
}

这假设会员总是有一次付款。如果可能有成员没有付款,您将不得不进行一些空值检查和左连接。

于 2013-06-19T09:39:45.077 回答