Tuesday, 10 September 2013

How to extract conditional statements from linq query?

How to extract conditional statements from linq query?

I want to be able to extract conditional statements from following query
inside a function. It's a linq to EF query. How should i do that? is there
any alternative way so that object oriented principles be applied (like
open/closed)?
var query = new UvwRequestAssignmentManagementBO().GetAll().Where(uvw
=> (uvw.FK_ProcessStep == 2)
&& (uvw.FK_Entity == SecurityContext.Current.User.FK_Entity)
&& (uvw.FK_Manager == 15))
.Select(p => new ReqSupAdminGridVm()
{
NameFamily = p.NameFamily,
RequestDate = p.RequestDate,
RequestNo = p.RequestNo,
RequestType = p.RequestType == 1 ?"a"
: (p.RequestType == 2 ? "b"
: (p.RequestType == 3 ? "c" :
(p.RequestType == 4 ? "d" : ""))),
RequestEvaluationStatus =
p.RequestEvaluationStatus_Aggregation == 1 ? "a"
:
(p.RequestEvaluationStatus_Aggregation
== 2 ? "b"
:
(p.RequestEvaluationStatus_Aggregation
== 3 ?"c"
:(p.RequestEvaluationStatus_Aggregation
== 4 ? "d" : ""))),
});
For Example instead of writing :
RequestType = p.RequestType == 1 ?"a"
: (p.RequestType == 2 ? "b"
: (p.RequestType == 3 ? "c" :
(p.RequestType == 4 ? "d" : ""))),
i want to be able to write this inside another class :
RequestType = ReqType.GetReqType(p.RequestType);
string GetReqType(int type){
switch(type){
case 1:
return "a";
case 2:
return "b";
}
}

No comments:

Post a Comment