how can i make a shortcut in *ngIf if i have alot of condition? - angular-ng-if

basically, i have an example of code like this:
<div *ngIf="key === 'key1' || key === 'key2' || key === 'key3'
|| key === 'key4' || key === 'key5' || key === 'key6' || key === 'key7'
|| key === 'key8' || key === 'key9' || key === 'key10' ">
<div *ngFor="let key of keyDatas">
<div>{{ key}}</div>
</div>
</div>
I want to use shortcuts instead of writing multiple lines of code. what is the best method here? I tried to do it using an array and it doesn't work. Can anyone help me?

Related

A or B or C query writing (Laravel blade)

I'm having problem writing A or B or C query in Laravel blade file.
I wrote below code but these code just show up as html sentece. Could you teach me correct code please?
#if (Auth::user()->br_name == 'A' || Auth::user()->br_name == 'B' || Auth::user()->br_name == 'C' )
#include('page.abc')
#endif

How to handle null values when trying to search a listview in xamarin forms

I am a beginner in xamarin forms. I am trying to sort a listview based on City, state, location name and zip code.
If one of these is empty , how can i search for the rest of the list ?
listView.ItemsSource = locs.Where(i => (i.City.ToLower().Contains(e.NewTextValue.ToLower())) || (i.State.ToLower().Contains(e.NewTextValue.ToLower())) || (i.LocationName.ToLower().Contains(e.NewTextValue.ToLower())) || (i.Zip.Contains(e.NewTextValue.ToLower()))) || locs.Contains(e.NewTextValue.ToLower());
use the null conditional operator, ?
locs.Where(i => (i.City?.ToLower().Contains(e.NewTextValue.ToLower()))
note the ? after City - if City is null then the rest of the statement will not execute
alternately, you could check for null explicitly
locs.Where(i => (iCity != null && i.City.ToLower().Contains(e.NewTextValue.ToLower()))

Writing LINQ Trying to create a LINQ to run in C# program

I'm currently creating a dropdown and hoping to find help writing a LINQ query...
Here is the SQL I'm trying to replicate:
select
IIf
(
TA.[LeaseDeptSubmittal] IS NOT NULL
AND TA.[LeaseDeptComplete] IS NULL, 'Lease Dept',
IIf
(
TA.[CompleteDate] IS NULL
AND TA.[RequestAddtnlInfo] IS NOT NULL, 'Need Info',
IIf
(
TA.[RequestDate] IS NULL, '',
IIf
(
TA.[RequestDate] IS NOT NULL
AND TA.[CompleteDate] IS NULL, 'Pending', 'Complete'
)
)
)
) ,
Id
from Tasks TA
You can translate IIF pretty directly into the C# (LINQ) ternary conditional operator, and then you can just follow my SQL to LINQ Recipe.
var ans = from TA in Tasks
select new {
Status = (TA.LeaseDeptSubmittal != null &&
TA.LeaseDeptComplete == null)
? "Lease Dept"
: (TA.CompleteDate == null &&
TA.RequestAddtnlInfo != null)
? "Need Info"
: TA.RequestDate == null
? ""
: (TA.RequestDate != null && TA.CompleteDate == null) ? "Pending" : "Complete",
TA.Id
};
I wouldn't normally indent ?: this way, but the nesting is quite deep (and unnecessarily redundant in one case) so I moved everything left.
I would re-work it like this to remove redundant tests and clear up the logic some:
var ans = from TA in Tasks
select new {
Status = (TA.LeaseDeptSubmittal != null &&
TA.LeaseDeptComplete == null)
? "Lease Dept"
: TA.CompleteDate != null
? "Complete"
: TA.RequestAddtnlInfo != null
? "Need Info"
: TA.RequestDate != null
? "Pending"
: "",
TA.Id
};

Practical case on Symfony2 validation constraint expression

I'm currently fighting with ExpressionLanguage validation regarding to a constraint wanted on form validation :
My form :
Text item
Entity1 item selector (dropdown)
Entity2 item selector (dropdown)
Textarea item
My wishes :
One error trigger on condition Text Item is empty and Entity1 selector is empty
Another error trigger on condition : Entity1.id == XX and Entity2.id == YY and Textarea is empty
So far, I got this in validation.yml :
Experveo\ShopBundle\Entity\Vehicle:
constraints:
- Expression:
expression: "this.getTextItem() != '' | this.getEntity1() != ''"
message: error1.
It seems I need to put the opposite condition to make it work. So far I didn't find any advanced clue on documentation, nor in expression language syntax help.
How can I achieve those conditions? Following is not working at all...
- Expression:
expression: >
this.getTextItem() == ''
&& this.getEntity1() != ''
&& this.getEntity1().getId() === 49
&& this.getEntity2() != ''
&& this.getEntity2() === 914
&& this.getTextAreaItem() == ''"
message: error2.
I finally found a solution. As I run on 2.4 and according to this fix merged later on, the ExpressionValidator was incorrectly skipping validation of null or empty string values.
Therefore, the callback option is a correct workaround, as mentionned in this other SO post. In my case :
/**
* #Assert\Callback
*
* Using a callback is a better solution insted of make an "Assert\Expression" for the class,
* because of a Symfony bug where the ExpressionValidator skip validating if the value if empty or null.
* Exemple for information : https://stackoverflow.com/questions/24649713/assert-expression-validation-not-working-at-attribute-level-in-symfony-2-4
*/
public function validate(ExecutionContextInterface $context)
{
if( '' === $this->getTextItem() && '' === $this->getMark() ){
$context->buildViolation("error 1")
->addViolation();
}
if( '' === $this->getTextItem()
&& '' === $this->getTextAreaItem()
&& (( $this->getEntity1() && $this->getEntity1()->getId() === self::ENTITY1_VALUE)
|| ($this->getEntity2() && $this->getEntity2()->getId() === self::ENTITY2_VALUE))
){
$context->buildViolation("error2")
->addViolation();
}
}

LINQ Lambda Where() not filtering as expected

I'm trying to build a query using LINQ for EF to filter results based on some basic logic. For some reason, even with the following Where() functions being executed and setting the right parameters, all data is being returned instead of the filtered results from Where().
I have run debug to make sure that my if() statements are indeed allowing the Where() to run when appropriate, and it is.
What am I missing?
var dbReports = db.SubmitReports;
if (Referee != String.Empty)
dbReports.Where(u => (u.Refree == Referee || u.Ar1Official == Referee || u.Ar2Official == Referee || u.FourthOfficial == Referee));
if (TeamName != String.Empty)
dbReports.Where(u => (u.HomeTeam == TeamName || u.VisitingTeam == TeamName));
if (PlayedOnStart != DateTime.MinValue && PlayedOnEnd != DateTime.MinValue)
dbReports.Where(u => (u.PlayedOn >= PlayedOnStart && u.PlayedOn <= PlayedOnEnd));
if (StateAssociation != String.Empty)
dbReports.Where(u => (u.StateAssociation == StateAssociation || u.StateAssociation2 == StateAssociation));
if (Division != String.Empty)
dbReports.Where(u => u.Division == Division);
if (ProfessionalLeague != String.Empty)
dbReports.Where(u => u.ProfessionalLeague == ProfessionalLeague);
if (AgeGroup != String.Empty)
dbReports.Where(u => u.AgeGroup == AgeGroup);
return dbReports.ToList();
Where doesn't modify the existing query - it creates a new query. You need to assign the result of the call to Where to something otherwise the result is simply discarded. Try this:
IQueryable<Report> dbReports = db.SubmitReports;
if (...)
{
dbReports = dbReports.Where(...);
}
You never use the return value of the Where method. Where does not modify the IEnumerable it is apply on but returns a Linq expression that will create a modify IEnumerable when executed (i.e when ToList is called).

Resources