Reputation: 3212
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";
}
}
Upvotes: 3
Views: 1035
Reputation: 4156
Create an extension method in a separate class, like this:
public static class ReqTypeExtension
{
public static string GetReqType(this int reqType)
{
switch (reqType)
{
case 1:
return "a";
case 2:
return "b";
case 3:
return "c";
case 4:
return "d";
}
return "";
}
}
use it like this:
RequestType = p.RequestType.GetReqType()
If you are set on this syntax:
RequestType = ReqType.GetReqType(p.RequestType);
Declare a static method on the ReqType class, like this:
public class ReqType {
public static string GetReqType(int reqType)
{
switch (reqType)
{
case 1:
return "a";
case 2:
return "b";
case 3:
return "c";
case 4:
return "d";
}
return "";
}
}
Upvotes: 1
Reputation: 393497
This should work
var query = new UvwRequestAssignmentManagementBO().GetAll().Where(uvw => (uvw.FK_ProcessStep == 2)
&& (uvw.FK_Entity == 1)
&& (uvw.FK_Manager == 15))
.Select(p => new ReqSupAdminGridVm
{
NameFamily = p.NameFamily,
RequestDate = p.RequestDate,
RequestNo = p.RequestNo,
RequestType = GetReqType(p.RequestType),
RequestEvaluationStatus = GetReqEvalStatus(p.RequestEvaluationStatus_Aggregation),
});
if you add the following local helper functions:
private static string GetReqEvalStatus(int agg)
{
switch (agg)
{
case 1: return "a";
case 2: return "b";
case 3: return "c";
case 4: return "d";
default: return "";
}
}
private static string GetReqType(int type)
{
switch (type)
{
case 1: return "a";
case 2: return "b";
case 3: return "c";
case 4: return "d";
default: return "";
}
}
See it working Live on IdeOne
If you want, you can move the helper functions into another class/file (http://ideone.com/EsK6W8):
RequestType = ReqType.GetReqType(p.RequestType),
RequestEvaluationStatus = ReqEvalStatus.GetReqEvalStatus(p.RequestEvaluationStatus_Aggregation),
And in separate file(s):
static internal class ReqEvalStatus
{
public static string GetReqEvalStatus(int agg)
{
switch (agg)
{
case 1: return "a";
case 2: return "b";
case 3: return "c";
case 4: return "d";
default: return "";
}
}
}
and
static internal class ReqType
{
public static string GetReqType(int type)
{
switch (type)
{
case 1: return "a";
case 2: return "b";
case 3: return "c";
case 4: return "d";
default: return "";
}
}
}
Upvotes: 0
Reputation: 4156
Check out PredicateBuilder, it lets you do what I think you are asking, I used it myself in a recent project and the code is reusable as well as readable.
I create predicates like this for my models:
using System;
using System.Linq;
using System.Linq.Expressions;
namespace MyModels
{
using Predicate = Expression<Func<Component, bool>>;
public partial class Component
{
public static Predicate HasKeywordContaining(string keyword)
{
return c => c.Keywords.Any(k => k.Value.Contains(keyword));
}
public static Predicate IsOwnedBy(string ownerName)
{
return c => c.OwnerName.Contains(ownerName);
}
public static Predicate HasPartNoContaining(string partNo)
{
return c => c.PartNo.Contains(partNo);
}
}
}
I can then use these to construct queries like this:
var whereComponent = PredicateBuilder.True<Component>();
whereComponent = whereComponent.And(Component.HasKeywordContaining(keyword));
whereComponent = whereComponent.And(Component.IsOwnedBy(ownerName));
var components = from c in db.Components.Where(whereComponent)
And of course this involves no client side filtering.
Upvotes: 2