Modifying a Linq query to add another condition - c#-4.0

I have a method below pulling specific fields that have null values.
Every record in the table has a status field of 1 or 0. 1 is active and 0 is inactive.
For the query below I only want to pull data for active records i.e status = 1
How can I modify this query to put in the condition for status = 1?
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}

You can chain two Where Linq extension method calls right after one another, like so:
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Where(t => t.status == 1) // added this line...
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}

you should just be able to change the where condition to the below
.Where(t => (t.FirstName == null || t.LastName == null || t.EmailAddress == null || t.Telephone == null) && t.status == 1)
The && means and, so its saying those fields are null and the status equals 1.

Related

How can i do a comparison in Groovy script?

I am comparing values in Groovy as follows:
if (creds.username == env.Username && creds.password == env.Password)
{
foundUser = true;
break;
}
It should return foundUser = true , but the issue is the second comparison, ( creds.password == env.Password ) if I take that out it works .
Any ideas as to why this may happen ?

Nested if statements...or use Switch Case?

I have a large nested if. One portion of the nested if works, however, in the second portion only some of the nested if works. I cannot get the switch case to work.
//Match Scoring //All work, when I flip Poker on top of Match the first and second ifs do not work
if playerMatchCardsArray[0] == playerMatchCardsArray[1] &&
playerMatchCardsArray[1] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] == playerMatchCardsArray[1] ||
playerMatchCardsArray[1] == playerMatchCardsArray[2] ||
playerMatchCardsArray[0] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] != playerMatchCardsArray[1] ||
playerMatchCardsArray[1] != playerMatchCardsArray[2] ||
playerMatchCardsArray[0] != playerMatchCardsArray[2] {
//Poker Scoring //All work, when I flip Poker on top of Match. However the following lines work do not work
// SUITED THREE OF A KIND //does not work
if firstRandomNumber == secondRandomNumber && secondRandomNumber == thirdRandomNumber {
// STRAIGHT FLUSH // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 &&
flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber{
// THREE OF A KIND // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber &&
pairFirstRandomNumber == pairThirdRandomNumber {
//STRAIGHT FORMULA // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 {
// SUITED PAIR // does not work
} else if firstRandomNumber == secondRandomNumber ||
secondRandomNumber == thirdRandomNumber ||
firstRandomNumber == thirdRandomNumber {
// PAIR // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber ||
pairFirstRandomNumber == pairThirdRandomNumber ||
pairSecondRandomNumber == pairThirdRandomNumber {
// FLUSH // works
} else if flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber {
Thank you in advance for your time and assistance. It's greatly appreciated!
It was the '}' that was causing the issue.

Netsuite: Server-side code not firing for csv uploads?

I created a script and deployed it to automatically populate web store fields when new inventory items are created in our system.
The code works when a new item is created through the interface, but does not when a new item is uploaded via csv.
This is the code:
function userEventAfterSubmit(type) {
if (type == 'create') {
var newItem = nlapiLoadRecord('inventoryitem', nlapiGetNewRecord().getId());
var storeDisplayImage = nlapiGetFieldValue('storedisplayimage');
if (storeDisplayImage == '' || storeDisplayImage == null)
newItem.setFieldValue('storedisplayimage', 620128);
var storeDisplayThumbnail = nlapiGetFieldValue('storedisplaythumbnail');
if (storeDisplayThumbnail == '' || storeDisplayThumbnail == null)
newItem.setFieldValue('storedisplaythumbnail', 620127);
var urlComponent = nlapiGetFieldValue('urlcomponent');
if (urlComponent == '' || urlComponent == null)
newItem.setFieldValue('urlcomponent', nlapiGetFieldValue('storedisplayname'));
var pageTitle = nlapiGetFieldValue('pagetitle');
if (pageTitle == '' || pageTitle == null)
newItem.setFieldValue('pagetitle', nlapiGetFieldValue('storedisplayname'));
var storeDescription = nlapiGetFieldValue('storedescription');
if (storeDescription == '' || storeDescription == null)
newItem.setFieldValue('storedescription', nlapiGetFieldValue('salesdescription'));
var storeDetailedDescription = nlapiGetFieldValue('storedetaileddescription');
if (storeDetailedDescription == '' || storeDetailedDescription == null)
newItem.setFieldValue('storedetaileddescription', nlapiGetFieldValue('salesdescription'));
var metaTagHtml = nlapiGetFieldValue('metataghtml');
if (metaTagHtml == '' || metaTagHtml == null)
newItem.setFieldValue('metataghtml', '<meta name="description" content="' + nlapiGetFieldValue('salesdescription') + '">');
nlapiSubmitRecord(newItem);
}
}
And then this function is called as the "After Submit Function". Am I not calling in this in the right place for it to run for csv uploads?
This is my script deployment:
Goto "Setup > Import/Export > CSV Import preferences"
Make sure " RUN SERVER SUITESCRIPT AND TRIGGER WORKFLOWS" is checked.

Formatting multiple line statement in vim

In vim a long statements like
if ( (image == null) || (image.getFileHash() == null) || (image.getFileImage() == null) )
gets indented like this when I hit an enter before the second OR
if ( (image == null) || (image.getFileHash() == null)
|| (image.getFileImage() == null) ) {
is there a way to set vim to indent it like
if ( (image == null) || (image.getFileHash() == null)
|| (image.getFileImage() == null) ) {
i.e., aligning it with the column after ( of the first previous line.
With dash-tom-bang's help was able to solve a part of the
problem. However, now the curly brackets are not aligned as expected
if { starts on the same line.
if ( (image == null) || (image.getFileHash() == null) ||
(image.getFileImage() == null) )
{
}
if ( (image == null) || (image.getFileHash() == null) ||
(image.getFileImage() == null) ) {
}
The setting that controls this is cinoptions, see :help cinoptions-values.
Add this to your .vimrc to align with the most local unclosed parenthesis. (You can give extra indent in spaces by giving a different number, including negatives. You can indent N tabstops by using Ns.)
set cinoptions=(0
The default value is 2s, which would indent two tabs past the open paren; hopefully this is what you're seeing. :)

Avoiding Multiple If's in c# - Best practise

Scenario:
Lets say we got to check for address lines. which includes addressline1, addressline2,Town,Country,Postcode
If any one of the property is entered, all other fields are mandatory.
If none of it is entered, the validation doesnt have to get trigged.
To achieve it, I ended up with two lines of If statement.
Like
if(AddressLine1 != null || AddressLine2 != null || Town != null || Country != null)
{
if(AddressLine1 != null && AddressLine2 != null && Town != null && Country != null) == false
{
return false;
}
}
Note: I am using c#. Are there any language constructs i can make use of.
private bool IsAddressValid(params string[] addressParts)
{
return addressParts.Any(p => p != null) ? addressParts.All(p => p != null) : true;
}
To be called like so:
var addressValid = IsAddressValid(AddressLine1, AddressLine2, Town, County);
Well, the null-coalescing operator can help with the first:
if (AddressLine1 ?? AddressLine2 ?? Town ?? Country != null)
{
if (AddressLine1 == null || AddressLine2 == null ||
Town == null || Country == null)
{
return false;
}
// Presumably there's more here
}
You might want to write some helper methods though:
if (IsAnyNonNull(AddressLine1, AddressLine2, Town, Country))
{
if (IsAnyNull(AddressLine1, AddressLine2, Town, Country))
{
return false;
}
}
Where the utility methods would be something like:
public static bool IsAnyNonNull(params object[] values)
{
return values.Any(x => x != null);
}
public static bool IsAnyNull(params object[] values)
{
return values.Any(x => x == null);
}
Of course, you've still got two if statements - but I think that's basically necessary here anyway.
If you make an array of the fields in the group, then you can do:
var fields = new object[] {AddressLine1, AddressLine2, Town, Country};
return fields.All(f => f == null) || fields.All(f => f != null);
Define this:
public static bool SameNullness(params object[] values)
{
int nullCount = 0;
foreach (var value in values)
{
if (value == null) nullCount++;
}
return nullCount == values.Length;
}
Then use it like:
SameNullness(AddressLine1, AddressLine2, Town, Country);

Resources