i want to use generic class to shorter my codes because
1) GetMaintData(int taskID) RelTypeId and RefMaintenance
2) GetAliSpReqs(int taskID) RelTypeId and RefAliSpReq
if you look below method you can see only TypeId and Ref data. i think that i can write new clear codes via generic class like that:
http://www.thereforesystems.com/dynamic-sort-with-linq/
public void GetData<TKey>(List<TaskRelation> cities, Func<TaskRelation, TKey> selector)
{
//include all GetMaintData,GetAliSpReqs,GetZone,GetAccess
}
public class EngGetCalculatedTaskField
{
private static TaskMaintenanceDataDataContext engTaskCtx { get; set; }
public EngGetCalculatedTaskField()
{
engTaskCtx = new TaskMaintenanceDataDataContext();
}
public string GetMaintData(int taskID)
{
var query = engTaskCtx.TaskRelations.Where(r => r.TaskId == taskID &&
r.RelTypeId == 12).Select(r => r.RefMaintenance.shortdesc);
return string.Join("; ", query.ToArray());
}
public string GetAliSpReqs(int taskID)
{
var query = engTaskCtx.TaskRelations.Where(r => r.TaskId == taskID
&& r.RelTypeId == 13)
.Select(r => r.RefAliSpReq.shortdesc);
return string.Join("; ", query.ToArray());
}
public string GetAccess(int taskID)
{
var query = engTaskCtx.TaskRelations.Where(r => r.TaskId == taskID
&& r.RelTypeId == 15)
.Select(r => r.RefAccessPanel.shortdesc);
return string.Join("; ", query.ToArray());
}
public string GetZone(int taskID)
{
var query = engTaskCtx.TaskRelations.Where(r => r.TaskId == taskID
&& r.RelTypeId == 14)
.Select(r => r.RefZone.shortdesc);
return string.Join("; ", query.ToArray());
}
Something like this:
Then use it like this:
Note that I’ve assumed LINQ to Objects – if this is LINQ to SQL (or something similar) then you should specify
IQueryable<TaskRelation>andExpression<Func<TaskRelation, string>>so that the filtering and projecting can be done at the data source.