Working with nullable [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wanted to minimize if block from below code.Please help me suitable extension method
var v = (from rec in _DataContext.tblCourierMasters
where rec.CourierReceievedDate == dtCourierReceivedDate
&& rec.RegionId == lRegionId
&& rec.PODNumber == strPODNo
select new { rec.TotalCafReceived, rec.ReceiptDoneCount }).FirstOrDefault();
lTPC = (long)v.TotalCafReceived;
if (v.ReceiptDoneCount== null) {
lRDC = -1;
}
else
lRDC = (long)v.ReceiptDoneCount;

You could use the null-coalescing operator:
lDRC = (long)(v.ReceiptDoneCount ?? -1);
So if v.ReceiptDoneCount is null, lDRC will be assigned the value of -1 instead.
Here's a demo.

Related

What is best replacement for a "foreach" loop that is executed only once? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am trying to refactor my code, my for each loop doesn't make much sense to use due to it being executed only once.
My question is, would it be best to replace it with an if statement, or is there a better way to do it?
int minReportNo = 0;
if (ValueUtil.IsNull(parentRow) || tableName == CompResultHeadEntity.EntityTableName)
{
foreach (CompResultHeadEntity header in dc.SelectEntities<CompResultHeadEntity>("", CompResultHeadEntity.ReportnoCol))
{
minReportNo = header.Reportno;
break;
}
if (minReportNo > 0)
minReportNo = 0;
}
So instead of using:
foreach (CompResultHeadEntity header in dc.SelectEntities<CompResultHeadEntity>("", CompResultHeadEntity.ReportnoCol))
should I use:
if(CompResultHeadEntity header in dc.SelectEntities<CompResultHeadEntity>("", CompResultHeadEntity.ReportnoCol))
You can replace your foreach loop with linq expression:
minReportNo = dc.SelectEntities<CompResultHeadEntity>(string.Empty, CompResultHeadEntity.ReportnoCol)?.FirstOrDefault()?.Reportno ?? minReportNo;
In case there's guarantee that there's always one non null element in the collection, linq expression can be simplified:
minReportNo = dc.SelectEntities<CompResultHeadEntity>(string.Empty, CompResultHeadEntity.ReportnoCol).First().Reportno;
Add the following namespace for linq to work.
using System.Linq;
P.S. I've also replaced your "" with string.Empty :)
P.P.S. I've just noticed that Camilo posted this same exact answer as a comment. Shout out to him 🍻
int minReportNo = 0;
if (ValueUtil.IsNull(parentRow) || tableName == CompResultHeadEntity.EntityTableName)
{
var result = dc.SelectEntities<CompResultHeadEntity>("", CompResultHeadEntity.ReportnoCol).FirstOrDefault();
if(result != null)
minReportNo = header.Reportno;
if (minReportNo > 0)
minReportNo = 0;
}

If I have multiple if else statement like this how do you consider refactor it? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
If I have multiple if else statement like this how do you consider refactor it?
var a = DoSomethingToGetA();
if (UserPreviouslySignedIn)
{
var b = DoSomethingToGetB();
if (a == b && !string.IsNullOrWhiteSpace(a))
{
Validate(a);
}
else
{
Validate(b);
}
}
else
{
Validate(a);
}
You could invert ifs and do early return to reduce nesting. Since you're doing the same call just with a different argument, you could also just determine the argument first and do a single call, something like this would do the same as your code I believe:
var a = DoSomethingToGetA();
if (!UserPreviouslySignedIn)
{
Validate(a);
return;
}
var b = DoSomethingToGetB();
var toValidate = (a == b && !string.IsNullOrWhiteSpace(a)) ? a : b;
Validate(toValidate);
But if you're checking whether a == b before validating a, and otherwise b, it would just give you the same result if you always validated b. If it is equal to a, then you're just validating a, but named b, if they're not, you're validating b anyway.
So the only differing factor here would be the UserPreviouslySignedIn boolean. Which would mean it could be boiled down to a ternary assignment and single Validate call as such:
var toValidate = !UserPreviouslySignedIn ? DoSomethingToGetA() : DoSomethingToGetB();
Validate(toValidate);

get a value from an object property and assign it to a C # string value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I have a below code to capture an objects values:
var key = fulfillment.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower().Contains("operator")).GetValue(fulfillment);
the code return:
the Operator property type is:
[JsonProperty(PropertyName = "operator")]
public object Operator { get; set; }
i want to get the name value of the index 1 -> OMS_OPERATOR_AUTOMATED and assign it to another string variable. How can i do this ?
Final answer after looking at code and data structure the answer was:
var foundOperator = (Dictionary<string, object>) fulfillment.Operator;
var teste = foundOperator["name"];

Null-coalesing or ternary operator prefernce? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = p_Shift_Id;
p_Shift_Id could be null. If it is null, then I want to use DBNull.Value. If it's not null, then the value that it contains.
What is the best approach? I would rather not use
if(p_Shift_Id == null)
{
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = DBNull.Value;
} else {
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = p_Shift_Id;
}
Cast p_Shift_Id as object and then use Null-Coalescing operator as follows:
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = (object)p_Shift_Id ?? DBNull.Value;

Whats the "Params" keyword in method parameter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Whats the "Default" keyword in this method?
public IEnumerable<T> createEmpty(Object key)
{
foreach (var item in MyCollection)
{
T element = new T();
element = default(T);
yield return element;
}
}
You mean the "Default" keyword?
Question already answered here: What does default(object); do in C#?
Thats only a method that returns the default value for that type, for example:
Int32 number = default(Int32); // returns 0
Object myObject = default(Object); // returns null
bool flag = default(bool); // return false

Categories