0

My code:

var Result = from TempRow in ArrAbsDiff where TempRow.Strike == StrikeOfMinAbsDiff select TempRow.PutMidPrice; 

I know that the above code return just one value of type decimal (maybe double). However, Result is of type Enumerable and I could not easily write Result + 2. I need to convert it property. I can do it through the following code:

var Result = (from TempRow in ArrAbsDiff where TempRow.Strike == StrikeOfMinAbsDiff select TempRow.PutMidPrice).Min(); 

Is there more efficient way to accomplish it?

Regards,

3 Answers 3

2

I know that the above code return just one value of type decimal

Then use First method instaed of Min.

Sign up to request clarification or add additional context in comments.

Comments

2

At compile time, it's unknown whether the query you have returns 1 record or more than 1 record. So, it assumes a list of them.

I don't know if there is a performance difference between them, I typically use .Single() to get the single record. It's more clear that I want a single record and not, for example, the minimum one.

Comments

1

Try using FirstOrDefault()" this selects the first result returned (as you know only one value will be returned). if a value is not returned the Result will be null, catch this in an if statement like below:

var Result = (from TempRow in ArrAbsDiff where TempRow.Strike == StrikeOfMinAbsDiff select TempRow.PutMidPrice).FirstOrDefault(); if (Result == null) return; // or error message 

this will also keep the type of value returned (typed this of the top of my head, may need slight changes!)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.