Linq syntax issue - List.Find(Predicate <T> match) - linq

I am trying to add some logic in this. But I am not sure how to add a second condition…
For instance:
foreach (FolderAssetInfo e in folderAssetsList)
{
var foundAsset = databaseAssetsList.Find(a => a.AssetFullName == e.AssetFullName);
//I want to add an AND logic inside the parenthesis such as:
//a => a.AssetFullName == e.AssetFullName && a.AssetFirstName == e.AssetFirstName)
if (foundAsset != null)
{
Console.WriteLine(Found it!);
}
}
How can I do that?

Your syntax is perfect:
var foundAsset = databaseAssetsList.Find(a => a.AssetFullName == e.AssetFullName && a.AssetFirstName == e.AssetFirstName);
Basically, the Predicate<T> is just syntax that will return a boolean value. Your syntax in the comment (a.AssetFullName == e.AssetFullName && a.AssetFirstName == e.AssetFirstName) will return a boolean, as written, so it will work fine for the predicate.
The one place where you do have a syntax error is your call to Console.WriteLine - this will require you to add quotation marks:
Console.WriteLine("Found it!"); // Quotes are required here!

Related

Declarative Pipeline - Use of when condition, how to do nested conditions anyOf/allOf/not

I am stuck at how to properly use nested conditions as proposed in the Jenkins syntax.
https://jenkins.io/doc/book/pipeline/syntax/#when
This is my current stage:
stage('Build'){
when{
anyOf{
allOf{
expression{env.BRANCH_NAME != 'master'}
expression{env.AUTO_BUILD == true && env.BUILD_OPT == snapshot && env.BRANCH_NAME !=~ /feature.+/}
}
expression{env.AUTO_BUILD == false}
}
}
steps{
echo env.AUTO_BUILD
echo env.BUILD_OPT
echo env.BRANCH_NAME
}
From my point of understanding is, if I set env.AUTO_BUILD = false, then this stage should be executed, since it is enclosed in an anyOf OR it would execute if my branch was e.g. develop and AUTO_BUILD = true, BUILD_OPT = snapshot.
However, this was not the case when I set AUTO_BUILD = false. The stage was not executed. Am I missing something?
There are two issues here in the declarative conditionals. First, looking at:
allOf {
expression{env.BRANCH_NAME != 'master'}
expression{env.AUTO_BUILD == true && env.BUILD_OPT == snapshot && env.BRANCH_NAME !=~ /feature.+/}
}
the issue here is that !=~ is not a valid operator for "does not match regular expression" in Groovy. You can replace it with !(env.BRANCH_NAME =~ /feature/) like so:
allOf {
expression{env.BRANCH_NAME != 'master'}
expression{env.AUTO_BUILD == true && env.BUILD_OPT == snapshot && !(env.BRANCH_NAME =~ /feature/)}
}
to achieve the behavior you desire.
Secondly, in the conditional:
expression{env.AUTO_BUILD == false}
the expression is checking for a boolean type in env.AUTO_BUILD. If the value is being assigned a string 'false', then the type check will fail and the conditional will not behave as expected. Inputting the environment env.AUTO_BUILD assignment as a boolean env.AUTO_BUILD = false and not a string env.AUTO_BUILD = 'false' will rectify this for you.

Blade inline if and else if statement

Is there a syntax to specify inline if and else if statement in Laravel blade template?
Normally, the syntaxt for if and else statement would be :
{{ $var === "hello" ? "Hi" : "Goodbye" }}
I would now like to include else if statement, is this possible?
{{ $var === "hello" ? "Hi" : "Goodbye" else if $var ==="howdie ? "how" : "Goodbye""}}
You can use this code in laravel blade:
{{ $var === "hello" ? "Hi" : ($var ==="howdie ? "how" : "Goodbye") }}
remember not every short code is a good one. in your example there's no single way to hit this else if because you're saying
if($var === "hello")
{
// if the condetion is true
"Hi";
}
else
{
// if the condetion is false
"Goodbye";
}
// error here
else if($var ==="howdie")
{ "how"; }
else
{ "Goodbye"; }
this's wrong you can't use two elses respectively. you've structure your conditions like
if (condition) {
# code...
} elseif (condition) {
# code...
} else {
}
the same in the ternary operators
(condition) ? /* value to return if first condition is true */
: ((condition) ? /* value to return if second condition is true */
: /* value to return if condition is false */ );
and beware of (,) in the second condition.
and as you see your code is just going to be tricky, unreadable and hard to trace. so use the if else if if you've more than one condition switching
and revise your logic.
<select id="days" class="Polaris-Select__Input" name="days" aria-invalid="false">
<option value="10" #if($settingsData->days == "10") selected #endif >at 10 Days</option>
</select>
#if($settingsData->days == "10") selected #else not selected #endif
with this code you can write single line if-else laravel blade with four condition.
{
{
$a == 10
? "10"
: $a == 20
? "20"
: $a == 30
? "30"
: $a == 40
? "40"
: "nothing";
}
}
$pandit->pandit_id != (auth()->user() ? "not pandit" : (auth()->guard('pandit')->user() ? auth()->guard('pandit')->user()->id : "vendor"
I believe that is two if else statements in one line. I cant imagine way to make it inline but i would have done something like this.
#if($var=="hello" || $var=="Hi")
{{$var === "hello" ? "Hi" : "Howdie"}}
#else
{{"Goodbye"}}
#endif

Using N-Grams to search within a table of string Xcode, swift 3

Below is my code for the current search, right now it only checks string by string. How do I implement ngram into it? This code checks an array of strings compared to what the user types in on the front end. It generates results with that string.
ngram is a solution i found online for what i need. However, what i want is the ability for users to search in the array without the use of spaces, say the array is "I love sushi and popcorn,...." and the user searches "I love popcorn" the array which consist of the string "I love sushi and popcorn" will not show up. However, I want it to be.
One option is to write a function that puts each string seperated by space into another array and have the search function run through a loop in that array and do this for each other strings. However, I find this inefficient.
Please let me know if there are other solutions. Thanks
func updateSearchResults(for searchController: UISearchController) {
// filter results
// reload table
let searchString:String = searchController.searchBar.text!
self.dataToDisplay = self.sampleData.filter({ (dataString:String) -> Bool in
let match = dataString.range(of: searchString, options: NSString.CompareOptions.caseInsensitive)
if match != nil {
return true
}
else {
return false
}
})
self.pdfArrSearch = self.pdfArr.filter({ (dataString:String) -> Bool in
let match = dataString.range(of: searchString, options: NSString.CompareOptions.caseInsensitive)
if match != nil {
return true
}
else {
return false
}
})

Codigniter validation for exact maching of string

I want to setup my validation rules in codeigniter such as a field starts with character 'P' or 'S', other wise it is invalid. How can I do that using Codigniter validation library?
Test Case 1: input: A145874 ------- invalid Must start with P or S
Test Case 2: input: P258741 ------- valid
Test Case 3: input: P45KK91 ------- invalid Must not contain Letters in other positions rather the first one.
Test Case 4: input: S457821 ------- valid
You would need to write a custom validation rule. Something like this:
public function check_first_char($str) {
$first_char = substr($str, 0, 1);
if ($first_char != 'P' || $first_char != 'S') {
$this->form_validation->set_message('check_first_char', 'The %s field must begin with P or S!');
return FALSE;
} else {
return TRUE;
}
}
Then you would add that validation rule like this:
$this->form_validation->set_rules('field_name', 'Field Name', 'callback_check_first_char');
The documentation explains it all pretty clearly.

T4 FieldName in camelCase without Underscore?

I'm using T4 to generate some class definitions and find that I'm getting an underscore in front of my field names.
I have set
code.CamelCaseFields = true;
just to be safe (even though I understand that's the default) but still end up with _myField rather than myField.
How can I generate a field name without the '_' character?
Also, where is the documentation for T4? I'm finding plenty of resources such as
Code Generation and Text Templates and numerous blogs, but I have not found the class-by-class, property-by-property documentation.
You're probably talking about EF4 Self Tracking Entities. The CodeGenerationTools class is included via the <## include file="EF.Utility.CS.ttinclude"#> directive, which you can find at "[VSInstallDir]\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude".
The FieldName function is defined as such:
private string FieldName(string name)
{
if (CamelCaseFields)
{
return "_" + CamelCase(name);
}
else
{
return "_" + name;
}
}
The "_" is hardcoded in the function. Coding your own shouldn't be difficult. Note that the CodeGenerationTools class is specific to this ttinclude file and isn't a generic and embedded way to generate code in T4.
I've written the following method to make first character upper case, remove spaces/underscores and make next character upper case. See samples below. Feel free to use.
private string CodeName(string name)
{
name = name.ToLowerInvariant();
string result = name;
bool upperCase = false;
result = string.Empty;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == ' ' || name[i] == '_')
{
upperCase = true;
}
else
{
if (i == 0 || upperCase)
{
result += name[i].ToString().ToUpperInvariant();
upperCase = false;
}
else
{
result += name[i];
}
}
}
return result;
}
input/output samples:
first_name = FirstName,
id = Id,
status message = StatusMessage
This is good advice however it doesn't help you in knowing WHERE the right place to put such a function is...
Is there any guidance on DECOMPOSING the EF .tt files or stepping through the output generation to see how it builds the output?
I was able to use the above function successfully by plugging it into a function called
(Ef4.3)
public string Property(EdmProperty edmProperty)
Which appears to be used to output the lines like "public int fieldname { get; set; }"
and changed the 3rd (index {2}) param to the formating to wrap with the function to modify the name, like this:
_typeMapper.GetTypeName(edmProperty.TypeUsage), //unchanged
UnderScoreToPascalCase(_code.Escape(edmProperty)), //wrapped "name"
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)), // unchanged
This is not perfect, eg: it doesn't keep existing "Ucasing" and doesn't care about things like this:
customerIP
outputs: Customerip
which IMO is not very readable...
but its better than what I WAS looking at which was a nightmare because the database was intermingled mess of camelCase, PascalCase and underscore separation, so pretty horrific.
anyway hope this helps someone...

Resources