I have three classes, they all have a property Date. I would like to write a generic class to return all the records for one date. Now the problem is: how can I write the lambda expression using generic type T?
The code simple is as below (i'll not compile, because "r.Date" would not working,but it's the effect that I'd like to achive)
Class GenericService<T>: IGenericService<T> where T:class
{
...
readonly IGenericRepository<T> _genericRepository;
public IEnumerable<T> GetRecordList(DateTime date)
{
var query=_genericRepository.FindBy(r=>r.Date=date);
}
Thank you for you help!
Regards, Léona
Write an interface which has IDate
and all of your entities must implement IDate
and write GenericService
like this :
public class GenericService<T>: IGenericService<T> where T : class, IDate
{
readonly IGenericRepository<T> _genericRepository;
public IEnumerable<T> GetRecordList(DateTime date)
{
var query=_genericRepository.FindBy(r => r.Date = date);
}
}
public interface IDate
{
DateTime Date{ set; get; }
}
public class Entity : IDate
{
DateTime Date { set; get; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With