I recently took over some code and came a across a statement that I wasn't sure what it was doing so I decided to post it.
currCarbon.CleanGeneration = (dynObj.#switch == 1 ? true : false);
Since 'dynObj' is a dynamic object holding a json value, is it checking if 'dynObj' contains a 'Switch' key / value?
Thanks for you help!
dynamic dynObj = GetValues();
if (dynObj != null) //json string from API
{
// Read the first record's carbon index
var prevCarbon = _dbContext.WatttimeApilog.OrderByDescending(c => c.CreatedDate).FirstOrDefault();
if (prevCarbon == null) //NO previous carbon value from database
{
//Plug in default values
currCarbon.Percentage = defaultPercent;
currCarbon.CleanGeneration = false;
currCarbon.Rating = defaultRating;
currCarbon.ValidUntil = DateTime.UtcNow;
insertFlag = true;
}
else
{
currCarbon.Percentage = dynObj.percent;
currCarbon.CleanGeneration = (dynObj.#switch == 1 ? true : false);
currCarbon.Rating = Convert.ToByte(dynObj.rating);
currCarbon.ValidUntil = dynObj.validUntil;
}
It's setting CleanGeneration to true or false based on whether or not #switch is equal to one via a ternary operator.
EDIT: The code isn't the most clean and can be shortened to just currCarbon.CleanGeneration = dynObj.#switch == 1;
Linked is docs on the ternary operator:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
Related
I've got a SQL DB column for DateTime Birthday on a customer object. The database allows that field to be null, but occasionally our users populate the data (from another system) with 1/1/1900.
I'd like to have my DatePicker field show nothing when the date in the DB is either null or DateTime.Parse("1900-01-01 00:00:00.000"). (It doesn't have to be fully blank, but can have the DatePicker field's standard default of " / / ".
I've been trying this:
dtBirthday.Value = customer.Birthday.HasValue ? customer.CustomerOptions.Birthday.Value : DateTime.Parse("1900-01-01 00:00:00.000");
That works fairly well for the null birthdays, but doesn't allow for the date in customer.Birthday actually being DateTime.Parse("1900-01-01 00:00:00.000").
Update
I did, in fact, google this, and attempted to use
var nullDate = DateTime.Parse("1900-01-01 00:00:00.000");
if (customer.Birthday.Value == nullDate)
{
dtBirthday.Format = eDateTimePickerFormat.Custom;
dtBirthday.CustomFormat = " ";
dtBirthday.Value = DateTime.FromOADate(0);
dtBirthday.Enabled = true;
}
else
{
dtBirthday.Format = eDateTimePickerFormat.Custom;
dtBirthday.CustomFormat = "M/d/yyyy";
dtBirthday.Value = customer.Birthday.Value;
dtBirthday.Enabled = true;
}
//dtBirthday.Value = customer.Birthday.HasValue ? customer.Birthday.Value : DateTime.Parse("1900-01-01 00:00:00.000");
if (customer.Anniversary.Value == nullDate)
{
dtAnniv.Format = eDateTimePickerFormat.Custom;
dtAnniv.CustomFormat = " ";
dtAnniv.Value = DateTime.FromOADate(0);
dtAnniv.Enabled = true;
}
else
{
dtAnniv.Format = eDateTimePickerFormat.Custom;
dtAnniv.CustomFormat = "MM/dd/yyyy";
dtAnniv.Value = customer.Anniversary.Value;
dtAnniv.Enabled = true;
}
//dtAnniv.Value = customer.Anniversary.HasValue ? customer.Anniversary.Value : DateTime.Parse("1900-01-01 00:00:00.000");
It does just about exactly what I want, except that the empty fields are completely disabled, even though I've specifically set them to enabled=true.
If I'm reading your question correctly, I think you want the text field to be empty when the birthday is actually null. Why not just populate it with an empty string:
txtBirthday.Value =
customer.Birthday.HasValue ?
customer.CustomerOptions.Birthday.Value.ToString() :
string.Empty;
After attempting several dozen different methods, none of which really did what I want, it occurred to me that I could just not set the value.
var nullDate = DateTime.Parse("1900-01-01 00:00:00.000");
if (customer.CustomerOptions.Birthday.HasValue && customer.CustomerOptions.Birthday.Value != nullDate)
{
dtBirthday.Value = customer.CustomerOptions.Birthday.Value;
}
if (customer.CustomerOptions.Anniversary.HasValue && customer.CustomerOptions.Anniversary.Value != nullDate)
{
dtAnniv.Value = customer.CustomerOptions.Anniversary.Value;
}
I understand that IndexOf returns -1 when the parameter isn't found, but this doesn't make sense to me.
I have this code that iterates over all the DevExpress Checkboxes on my form, checks to see what their tag is, and tries to find it in the "filter" parameter that was passed to the method. If the tag is found in the filter, it should check that checkbox. It is always equating to false.
public void FilterChanged(Control.ControlCollection controls, string filter)
{
filter = filter.Replace("[", String.Empty);
filter = filter.Replace("]", String.Empty);
foreach (Control control in controls)
{
if (control is CheckEdit && control.Tag != null)
{
var c = (CheckEdit)control;
var cFilter = c.Tag.ToString();
cFilter = cFilter.Replace("(", String.Empty);
cFilter = cFilter.Replace(")", String.Empty);
if (filter.ToUpper().IndexOf(c.Tag.ToString().ToUpper()) >= 0)
c.Checked = true;
else
c.Checked = false;
}
}
}
I set a breakpoint on my inner IF statement, and in my Immediate Window I entered the following:
filter.ToUpper().IndexOf(c.Tag.ToString().ToUpper()) = -1
filter.ToUpper() = "FILE NOT LIKE '%VERSIONINFO.CS%'"
cFilter.ToUpper() = "FILE NOT LIKE '%VERSIONINFO.CS%'"
Those look like pretty much the exact same thing, so shouldn't it be returning 0?
I cannot use equals because the filter might include multiple clauses, and thus wouldn't be equal.
cFilter.ToUpper() = "FILE NOT LIKE '%VERSIONINFO.CS%'"
Those look like pretty much the exact same thing, so shouldn't it be returning 0?
But you are using c.Tag.ToString() instead of cFilter.ToUpper().
So this should work as expected:
if (filter.ToUpper().IndexOf(cFilter.ToUpper()) >= 0)
c.Checked = true;
else
c.Checked = false;
Note that you should use StringComparison.OrdinalIgnoreCase instead in IndexOf
c.Checked = filter.IndexOf(cFilter, StringComparison.OrdinalIgnoreCase) >= 0;
I am trying to make a dynamic linq query that will check for values based on a string.
First of all, here's the query:
objQry = from o in m_Db.OBJECTS.Where(whereConditions)
select o;
if(!objQry.Any())
{
return null;
}
The whereConditions variable is a string I build and pass as parameter to find out the values I need. Here's examples of valid string:
OBJ_NAME == \"Sword\" and OBJ_OWNER == \"Stan\"
This will return any item whose name is "Sword" and owner is "Stan;
OBJ_COLOR == \"Blue\" OR OBJ_COLOR == \"Red\"
This will return any item which color is either blue or red.
Up to there, I'm fine, but now I have a problem: I need to check a decimal field. So I've tried this string:
OBJ_NUMBER == 1
But the query returns null even if there are objects which OBJ_NUMBER value is 1. It's a decimal. How can I indicate the query that they need to check for a decimal value?
**** EDIT ****
I have tried to "modify" the value passed so that it looks like this:
"CARD_NUMBER == Convert.ToDecimal(1)"
And now I have a different kind of error telling me this:
LINQ to Entities does not recognize the method 'System.Decimal ToDecimal(Int32)' method, and this method cannot be translated into a store expression.
Any clues anyone? I'm still looking for a way to do this. Thanks!
EDIT 2
You can get an example of how my code is shaped by looking at this question.
Let's come back at this problem. I want to check decimal values. Let's say that OBJ_NUMBER is a decimal field.
Using Dynamic Linq, I tried to read the decimal field. Say that I want to get each object which number is 1.27. The whereConditions field would then be shaped like this:
OBJ_NUMBER == 1.27
But then I would get an Invalid real literal '1.27' error. I don't know why.
So I have tried Gert Arnold's solution and done this instead:
decimal bDecimal = decimal.Parce(valueToParse);
param = new ObjectParameter("cardNumber", typeof(decimal)) { Value = bDecimal };
valuesToUse.Add("CARD_NUMBER == #cardNumber");
listParams.Add(param);
But I ended up having 2 problems:
The first problem is that my whereConditions string is shaped this way:
CARD_NUMBER == #cardNumber
But I get the following error:
No property or field 'cardNumber' exists in type 'CARD'
Leading me to believe that it cannot make the link between the object parameter and the string used to do the query.
As you can see, I have a list of Params. This is because I cannot know for sure how many parameters the user will chose. So each time the user enters a new search field, I have to create a new ObjectParameter and store it in a list. Here's how I try to do the thing after:
ObjectParameter[] arrayParameters = listParams.ToArray();
// Convert the list to an array
And then, when I try to make the query:
cardQry = from c in mDb.CARD.Where(whereConditions, arrayParameters)
select c;
But to no avail.
RESULTS
Based on the answered question below, I have developped something "awful", yet functional.
First of all, I ignore every decimal fields because I could never reach them with dynamic linq. Instead, I do this:
var valuesToParse = keyValuePair.Value.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
// Here I parse the value and, if that's the case, the symbol.
decimal baseValue = decimal.Parse(valuesToParse[0]);
if (valuesToParse.Count() > 1)
{
string baseMethod = valuesToParse[1];
if (baseMethod == ">" || baseMethod == ">=")
{
if (baseMethod == ">=")
{
baseValue--;
}
// The list is actually like this: Dictionary<string, object> list = new Dictionary<string, object>();
list.Add("low", baseValue);
// I kind of activate a tag telling me that the user is looking for a higher value.
cardHigher = true;
}
else
{
if (baseMethod == "<=")
{
baseValue++;
}
list.Add("low", baseValue);
cardLower = true;
}
}
else
{
//lowParam = new ObjectParameter("dec", typeof(decimal)) { Value = baseValue };
list.Add("low", baseValue);
}
cardNumberActivated = true;
At the end, when I get the list of objects, I do this:
if (list.Count > 0)
{
(example)
if (cardNumberActivated)
{
if (cardHigher)
{
q = mDb.CARD.Where("CARD_NUMBER >= #0", list["low"]).ToList();
}
else if (cardLower)
{
q = mDb.CARD.Where("CARD_NUMBER <= #0", list["low"]).ToList();
}
else
{
q = mDb.CARD.Where("CARD_NUMBER == #0", list["low"]).ToList();
}
}
}
// Here we get the orinalData with the basic filters.
listToReturn.AddRange(cardQry);
if (q != null)
{
//listToReturn.AddRange(q);
for (int i = 0; i < listToReturn.Count; i++)
{
var priceList1 = listToReturn[i];
if (!q.Any(_item => _item.CARD_NUMBER == priceList1.CARD_NUMBER))
{
listToReturn.RemoveAt(i);
i--;
}
}
}
And it works. This is not an elegant way to make it work, but I can validate the fields the way I wanted, and for this, I am thankful at last.
You should not build a query string with inline predicate values. Use parameters in stead. Then will also be able to specify the type:
var whereConditions= "it.CARD_NUMBER = #cardNumber";
var param = new ObjectParameter("cardNumber", typeof(decimal)) { Value = 1 };
objQry = from o in m_Db.OBJECTS.Where(whereConditions, param);
Edit
I don't know what doesn't work in your code. Here's just a random piece of working code derived from one of my own projects:
var param1 = new ObjectParameter("dec", typeof(decimal)) { Value = 90000m };
var param2 = new ObjectParameter("int", typeof(int)) { Value = 90000 };
var q = ValueHolders.Where("it.DecimalValue >= #dec OR it.IntegerValue > #int",
param1, param2).ToList();
Note that param1, param2 could also be an array of ObjectParameter.
I am trying to use a nice near object setter, but have null issues.
My code right now:
var Result = new RefundReplyObject
{
AuthorisationNumber = reply.refundResponse.transactionDetails.authorisationNumber,
ChargeValue = reply.refundResponse.transactionDetails.totalAmount.amount,
Message = reply.refundResponse.transactionDetails.message,
ReconciliationReference = reply.refundResponse.transactionDetails.reconciliationReference,
SettlementDate = reply.refundResponse.transactionDetails.settlementDate,
Status = TransactionStatusToLocalModel(reply.refundResponse.transactionDetails.status),
TransactionReference = reply.refundResponse.transactionDetails.transactionReference
};
BUT ... 'totalAmount' might be null. So, I get errors.
Is there a neat way to handle this, so that if 'totalAmount' is null, then set chargevalue to zero?
How about a ternary operator, that checks to see if total amount is null. If it isn't, then use it's amount, otherwise 0.
ChargeValue = (reply.refundResponse.transactionDetails.totalAmount != null) ? reply.refundResponse.transactionDetails.totalAmount.amount : 0,
You could do, for example:
ChargeValue = reply.refundResponse.transactionDetails.totalAmount != null ? reply.refundResponse.transactionDetails.totalAmount.amount : 0
ternary operator to the rescue!
ChargeValue = totalAmount ? totalAmount.amount : 0;
Something like this?
ChargeValue = (null == reply.refundResponse.transactionDetails.totalAmount)
? 0
: reply.refundResponse.transactionDetails.totalAmount.amount
You're not going to be able to do it on the setter side of things since the exception is occuring before you even get there. You can get what you're after (and clean up your code substantially in the process) by doing something like this:
var trans = reply.refundResponse.transactionDetails;
var Result = new RefundReplyObject
{
AuthorisationNumber = trans.authorisationNumber,
ChargeValue = trans.totalAmount == null ? 0 : trans.totalAmount.amount,
Message = trans.message,
ReconciliationReference = trans.reconciliationReference,
SettlementDate = trans.settlementDate,
Status = TransactionStatusToLocalModeltrans.status),
TransactionReference = trans.transactionReference
};
Hi guys, probably a simple one.
Using C# .Net 4.0 and Visual Studio 2012 Ultimate.
Got the following code:
string part = "";
part = txtIOpart.Text;
txtBatchCV.Text = txtBatchIO.Text;
txtPartCV.Text = part;
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg);
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal();
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS();
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal();
txtBarCV.Text = "*" + Sqlrunclass.SplitInfo_ASno(part, pg) + "*";
txtBarNumCV.Text = txtBarCV.Text;
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location();
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc();
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height();
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter();
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit();
picTypeCV.Image = ftpclass.Download("CVspecType" + Sqlrunclass.SplitSpec_TypeCV() + ".jpg", "ftp.shaftec.com/Images/TypeJpg", "0095845|shafteccom0", "4ccc7365d4");
if (txtBatchCV.Text == null || txtBatchCV.Text == "")
{
txtBatchCV.Text = "ALL";
}
As you can see at the bottom I'm checking the batch, but I need to check all of the data thats being set by a bunch of methods. Each one will have a different txt output if it sees a null or blank txt. Is there anyway to shorten this code?
Try, txtBatchCV.Text For example
//Just for null
txtBatchCV.Text = (txtBatchCV.Text ?? "ALL").ToString();
//for both null and empty string
txtBatchCV.Text = string.IsNullOrEmpty(txtBatchCV.Text) ? "ALL": txtBatchCV.Text;
You could iterate through all the textboxes
foreach (var txt in form.Controls.OfType<TextBox>())
{
switch(txt.Id){
case "txtBatchCV":
// Do whatever you want for txtBatchCV e.g. check string.IsNullOrEmpy(txt.Text)
break;
}
}
I borrowed the above from here:
How do I loop through all textboxes and make them run corresponding actions from action dictionary?
In response to the comment I got from Tim, I've added a bit more code to explain what you could do. My code example was never meant to be a full solution.
TextBox.Text is never null, it will return "" then. If your methods return null you could use the null-coalescing operator:
string nullRepl = "ALL";
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg) ?? nullRepl;
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal() ?? nullRepl;
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS() ?? nullRepl;
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal() ?? nullRepl;
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location() ?? nullRepl;
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc() ?? nullRepl;
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height() ?? nullRepl;
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter() ?? nullRepl;
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit() ?? nullRepl;
For starters you could use string.IsNullOrEmpty(txtBatchCV.Text), it's a convevience method that basically does what you do in the if check.
You can atleast use one of these methods:
string.IsNullOrEmpty(txtBatchCV.Text)
or
string.IsNullOrWhitespace(txtBatchCV.Text)
I would try something like this:
void SetDefaultIfNull(TextBox txt, string defaultVal)
{
if (string.IsNullOrWhitespace(txt.Text))
txt.Text = defaultVal;
}
Then pass each textbox and the default to the method.