I have remote context and I know that classes in this context have the same structure. I want to avoid creating method for each class. Example
public IEnumerable<CommonPersonFeatureValue> GetRegulation()
{
var query = from c in remoteContext.RegulaminDaneOsobowe
select new CommonPersonFeatureValue
{
PersonId = c.PersonId ?? 0,
Crated = c.Created ?? DateTime.MinValue,
CurStatus = String.IsNullOrEmpty(c.CurStatus) ? c.CurStatus : String.Empty,
NewStatus = String.IsNullOrEmpty(c.NewStatus) ? c.NewStatus : String.Empty,
Modified = c.Modified ?? DateTime.MinValue
};
return query;
}
public IEnumerable<CommonPersonFeatureValue> GetNDA()
{
var query = from c in remoteContext.NDA
select new CommonPersonFeatureValue
{
PersonId = c.PersonId ?? 0,
Crated = c.Created ?? DateTime.MinValue,
CurStatus = String.IsNullOrEmpty(c.CurStatus) ? c.CurStatus : String.Empty,
NewStatus = String.IsNullOrEmpty(c.NewStatus) ? c.NewStatus : String.Empty,
Modified = c.Modified ?? DateTime.MinValue
};
return query;
}
Is it possible to create method like this
public IEnumerable<CommonPersonFeatureValue> GetCommonPersonFeatureValue<T>(this System.Data.Services.Client.DataServiceQuery<T> items)
The problem is that I don't know how to properly access property of generic object. One solution is use this code
string newStatus = item.GetType().GetProperty("NewStatus").GetValue(item) as string
But I think that it is weak approach. Do you have better solution to replace these two methods in one common method ?