Reputation: 17288
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());
}
Upvotes: 1
Views: 91
Reputation: 1501053
Something like this:
public string GetList(IEnumerable<TaskRelation> relations,
int taskId, int relTypeId,
Func<TaskRelation, string> projection)
{
var query = relations.Where(r => r.TaskId == taskId &&
r.RelTypeID == relTypeId)
.Select(projection));
.ToArray()
return string.Join("; ", query.ToArray());
}
Then use it like this:
string zones = GetList(reslations, taskID, 14, r => r.RefZone.shortdesc);
Note that I've assumed LINQ to Objects - if this is LINQ to SQL (or something similar) then you should specify IQueryable<TaskRelation>
and Expression<Func<TaskRelation, string>>
so that the filtering and projecting can be done at the data source.
Upvotes: 1