Refactor If / Elseif - julia

I have a function with a lot of if/elseif statements. I'm looking for a way to improve the code of this function, replacing IF/ELSEIF. I`m trying to use dictionaries, but I am not sure if it is the best way. Improve readability is important as well.
if lowestDim == "Campaign" || lowestDim == "Adgroup"
if campaignType != "PERFORMANCE_MAX"
drillThroughMap["DRILLLINK5"] = Dict(
"filters" => lowestDim == "Campaign" ? Dict([
SQLFilter("CAMPAIGN", "=", onlyCampaign), SQLFilter("SOURCE", "=", source)]) : Dict([
SQLFilter("CAMPAIGN", "=", onlyCampaign), SQLFilter("SOURCE", "=", source), SQLFilter("ADGROUP", "=", adgroup)]),
"drillTo" => demographicDashboard
)
end
drillThroughMap["DRILLLINK10"] = Dict(
"filters" => Dict(),
"drillTo" => campaignComparisonDashboard
)
end
if presetDateRange != "Last 12+ Months" && (lowestDim != "Keyword" ) && !(source == "google" && (campaignType == "SMART" || campaignType == "PERFORMANCE_MAX"))
drillThroughMap["DRILLLINK6"] = Dict(
"filters" => Dict([SQLFilter("CAMPAIGN_FROM_DIAGNOSIS", "=", lowestDim == "Campaign" ? campaignStr : onlyCampaign * " ·· " * source)]),
"drillTo" => adgroupComparisonDashboard
)
end
if presetDateRange != "Last 12+ Months" && lowestDim != "Campaign" && (campaignType == "SEARCH")
drillThroughMap["DRILLLINK7"] = Dict(
"filters" => Dict([SQLFilter("ADGROUP_FROM_DIAGNOSIS", "=", adgroup * " ·· " * onlyCampaign * " ·· " * source)]),
"drillTo" => keywordComparisonDashboard
)
elseif presetDateRange != "Last 12+ Months" && lowestDim == "Campaign" && (campaignType == "SEARCH")
drillThroughMap["DRILLLINK7"] = Dict(
"filters" => Dict([SQLFilter("CAMPAIGN_FROM_DIAGNOSIS", "=", onlyCampaign * " ·· " * source)]),
"drillTo" => keywordComparisonDashboard
)
end
Just a piece of the function code.

Related

ASP.NET MVC Entity Framework 5 get true,false or both result

I have a table that I am filtering on. There is a four filters status, project, department, KPI
KPI is a boolean value. This filter has three value. True, False or both.
So, when the filter is true, it should return 'true' data, when the filter is 'false', return 'false' data; and when 'all' return both true and false data.
My code attached below, how can I enhance this code ? Can I write this with out if else conditions
var result = new List<TaskMaster>();
bool kpidata;
if (request.IsKPI == 1) { kpidata = true; } else { kpidata = false; }
//For KPI Tasks
if (request.IsKPI == 1 || request.IsKPI == 0)
{
result = await _context.TaskMaster.Where(c => c.IsActive && (c.Status == request.Status || request.Status == "All") && (c.ProjectId == request.ProjectId || request.ProjectId == 0) && (c.IsKPI == kpidata) && (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)).Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery().ToListAsync();
//For Non-KPI Tasks
}
else {
result = await _context.TaskMaster.Where(c => c.IsActive && (c.Status == request.Status || request.Status == "All") && (c.ProjectId == request.ProjectId || request.ProjectId == 0) && (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)).Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery().ToListAsync();
}
Revise the code, both queries are almost identical except c.IsKPI == kpidata part.
Few ways to remove the redundant:
Write the IQueryable query and append the .Where() if the condition meets. You are fine to append/extend the query before the query is materialized (via .ToList(), .First() etc.).
var result = new List<TaskMaster>();
bool kpidata;
if (request.IsKPI == 1) { kpidata = true; } else { kpidata = false; }
IQueryable<TaskMaster> query = _context.TaskMaster
.Where(c => c.IsActive
&& (c.Status == request.Status || request.Status == "All")
&& (c.ProjectId == request.ProjectId || request.ProjectId == 0)
&& (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0));
//For KPI Tasks
if (request.IsKPI == 1 || request.IsKPI == 0)
{
query = query.Where(c => c.IsKPI == kpidata);
}
result = await query.Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery()
.ToListAsync();
You can also migrate the if logic to the query too.
&& (!(request.IsKPI == 1 || request.IsKPI == 0) || c.IsKPI == kpidata)
If !(request.IsKPI == 1 || request.IsKPI == 0) returns true, the c.IsKPI == kpidata part will be omitted.
result = await _context.TaskMaster
.Where(c => c.IsActive
&& (c.Status == request.Status || request.Status == "All")
&& (c.ProjectId == request.ProjectId || request.ProjectId == 0)
&& (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)
&& (!(request.IsKPI == 1 || request.IsKPI == 0) || c.IsKPI == kpidata))
.Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery()
.ToListAsync();
This
(request.IsKPI == 1 || request.IsKPI == 0)
can also be revised to:
(new List<int> { 0, 1 }).Contains(request.IsKPI)

arg1 is not of type xs:anyAtomicType

return
fn:concat (fn:string-join ((
"somevalue.1.",
"somevalue.2.",
"some val 3",
"some val4",
$somevariable), " "),
for $i in $loopvar
if ((fn:exists($loopvar)) and (fn:count($loopvar) > 1)) then
" where ( " || $loopvar[i] || " and "
else if(fn:exists($loopvar) and (fn:count($loopvar) > 0)) then
" where " || $loopvar[i]
else() )
I m trying the above code but its giving me error at the fn:concat line. Can anyone help here?
The fn:concat() function takes any number of single strings and not a sequence.
The fn:string-join() function concatenates sequences.
So, one solution would be to include the for loop in the sequence passed to string-join(), as in:
fn:concat(fn:string-join((
"somevalue.1.",
"somevalue.2.",
"some val 3",
"some val4",
$somevariable,
for $i in $loopvar
return
if ((fn:exists($loopvar)) and (fn:count($loopvar) > 1)) then
"where ( " || $loopvar[i] || " and "
else if(fn:exists($loopvar) and (fn:count($loopvar) > 0)) then
"where " || $loopvar[i]
else() )
), " ")
Hoping that helps,

Expressions in a querybuildRange

Hi I am trying to do this query:
Select ProjInvoiceJour
where NOT (ProjInvoiceJour.ProjInvoiceType == ProjInvoiceType::Onaccount && ProjInvoiceJour.CountryRegionID != "ES")
But I need to do it whit querybuilder:
qbds.addRange(fieldnum(ProjInvoiceJour, ProjInvoiceType)).value(strfmt('!%1',
strfmt("((%1.%2 == %3) && (%4.%5 != '%6'))",
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, ProjInvoiceType),
any2int(ProjInvoiceType::OnAccount),
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, CountryRegionID),
queryvalue("ES"))));
But the query has some error:
SELECT * FROM ProjInvoiceJour WHERE ((NOT (ProjInvoiceType = 255)))
Thanks
The law of De Morgan comes to rescue:
select ProjInvoiceJour
where !(ProjInvoiceJour.ProjInvoiceType == ProjInvoiceType::Onaccount &&
ProjInvoiceJour.CountryRegionID != 'ES')
is equivalent to:
select ProjInvoiceJour
where ProjInvoiceJour.ProjInvoiceType != ProjInvoiceType::Onaccount ||
ProjInvoiceJour.CountryRegionID == 'ES'
Or in a query:
qbds.addRange(fieldnum(ProjInvoiceJour, ProjInvoiceType)).value(strfmt('((%1.%2 != %3) || (%4.%5 == "%6"))',
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, ProjInvoiceType),
0+ProjInvoiceType::OnAccount,
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, CountryRegionID),
'ES'));

Post wall facebook Autoit

I'm learning Autoit and have searched for a UDF as below, expecting people to help
Func FB_Post($Handle, $Content, $Pri = 0, $UID = False)
Local $Post_pri[3] = ["300645083384735", "291667064279714", "286958161406148"], $data, $UserID = ($UID ? $UID : $Handle[1]), $Post_body, $Get_id
If not IsArray($Handle) then Return SetError(1, 0, False)
If $Pri < 0 Or $Pri > 2 or (not IsNumber($Pri)) Then $Pri = 0
$data = "privacyx=" & ($UID ? "" : $Post_pri[$Pri]) & "&xhpc_targetid=" & $UserID &"&xhpc_message=" & _URIEncode($Content) & "&fb_dtsg=" & $Handle[2]
$Post_body = _HttpRequest(2, "https://www.facebook.com/ajax/updatestatus.php",$data, $Handle[0], "https://www.facebook.com/profile.php?id=" & ($UID ? $UID :$Handle[1]))
$Get_id = StringRegExp($Post_body, "top_level_post_id":"(.*?)"",3)
If #error then Return SetError(2, 0, True)
Return $Get_id[0]
EndFunc
Click Full Source

Is there an overhead with tracking when using EF6.1 and if so how can I remove this?

I have some complex queries like this:
var contents = await db.Contents
.Where(q => q.ContentStatusId == contentStatusId || contentStatusId == 0)
.Where(q => q.ContentTypeId == contentTypeId || contentTypeId == 0)
.Where(q => q.CreatedBy == contentCreatedBy || contentCreatedBy == "0")
.Where(q => q.ModifiedBy == contentModifiedBy || contentModifiedBy == "0")
.Where(q => q.SubjectId == subjectId)
.ToListAsync();
I send the results back to my web API and I do not need any tracking. Does EF automatically add tracking and if so is that an overhead that I do not need. Also if it does then is there a way I can turn off the tracking ?
Yes there is an overhead. You can use AsNoTracking() on IQueryable or your DbSet.
var contents = await db.Contents
.Where(q => q.ContentStatusId == contentStatusId || contentStatusId == 0)
.Where(q => q.ContentTypeId == contentTypeId || contentTypeId == 0)
.Where(q => q.CreatedBy == contentCreatedBy || contentCreatedBy == "0")
.Where(q => q.ModifiedBy == contentModifiedBy || contentModifiedBy == "0")
.Where(q => q.SubjectId == subjectId)
.AsNoTracking()
.ToListAsync();

Resources