Seems like what you want (retrive fees depeding on dates and members) can be fully handle in sql statement, so manage yourself to do only one SqlCommand instead of doing many queries handled by C# code.
select *
from
members m
, fees f
where
m.m_id=f.m_id
and datediff(day, f.f_start_date, getdate()) > 30
To retrieve all members with all fees linked but only if start date exceeded last 30 days (ofc, retrieve paid fees..)
I don't understood what is the condition that make one fee "the last fee" of a member.
If f_end_date
is set if the fee is paid it would be something like :
select *
from
members m
, fees f
where
m.m_id=f.m_id
and datediff(day, f.f_start_date, getdate()) > 30
and f_end_date is null
If fees existing in fees are just current fees to paid, then manage only the last fee by date :
select *
from
members m
, ( SELECT m_id,max(f_start_date) as f_start_date
FROM fees
GROUP BY m_id) f_lastfees
,fees f
where
m.m_id=f.m_id
and f.m_id = f_lastfees.m_id
and f.f_start_date = f_lastfees.f_start_date
and datediff(day, f.f_start_date, getdate()) > 30
Will retrieve only members who didn't paid the last fees over last 30 days
Just comment above if you need some help in sql, but anyway try to retrieve directly the information you need in sql statement, it's better for performance and makes code more readable.
Hope it helps ;)
EDITED
The new table fees will contains :
f_id
f_amount
f_creation_date --used to know if fee is not paid over last 30days
f_paid_date --used to know if fee is paid or not (equals null if not paid)
Final request looks like
select *
from
members m
,fees f
where
m.m_id=f.m_id
and datediff(day, f.f_creation_date, getdate()) > 30
and f_paid_date is null
Will retrieve only merbers who didn't paid the last fees over last 30 days according to alter table made on fees table