What is the difference between First and FirstOrDefault , Last and LastOrDefault [duplicate] - linq

This question already has answers here:
When to use .First and when to use .FirstOrDefault with LINQ?
(14 answers)
Closed 6 years ago.
i am working with LINQ there i use First and FirstOrDefault both return the same result what is the difference

First and Last throw an exception if no result was found.
FirstOrDefault and LastOrDefault return the result variable type's default value in that case.

FirstOrDefault and LastOrDefault would not throw an exception if there are no search item in sequence, they will return null.

Related

How to perform Operations on Optional OfNullable and handle Null [duplicate]

This question already has answers here:
Should Optional.ofNullable() be used for null check?
(3 answers)
Null check using Optional
(1 answer)
Null check vs Optional isPresent check
(3 answers)
Uses for Optional
(14 answers)
Closed 2 years ago.
I've a nullable field where I want to perform operation, if not null. return null otherwise.
I can write it in plain Java as
return (myfield != null)? myfield.getData() :null;
How can I write the same code in one line with Optional
Something like below.
Below code is not a correct code, just want to ask the correct version of it
return Optional.ofNullable(myfield).map(Functionas.identity().getData()).orElse(null);

Golang var and literal usage [duplicate]

This question already has answers here:
nil slices vs non-nil slices vs empty slices in Go language
(3 answers)
Closed 11 months ago.
Option 1
var employees []Employee
session.Employees = employees
Option 2
session.Employees = []Employee{}
What, if any, is the difference in the two Golang code options, with respect to session.Employees after execution?
Your first version assigns the value of the employees variable to session.Employees. It will be the zero value of the type []Employee, which is nil. You could simply write this as:
session.Employees = nil
Your second version assigns the value of a composite literal, which will be an empty (length=0, capacity=0) but non-nil slice.
See related questions:
nil slices vs non-nil slices vs empty slices in Go language
What is the point of having nil slice and empty slice in golang?
Correct way to initialize empty slice

Ruby what does star prefix means in a loop variable? [duplicate]

This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end
It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".

Decrementing For() loop [duplicate]

This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 6 years ago.
What is the syntax of a decrementing for() loop?
I would like to do something like the following without getting a compilation error:
for ndx in 5...4 {
print("do something")
}
This prints the value of i in reverse order:
for i in (1...10).reversed() {
print(i)
}

Finding the index of a particular element in an array with duplicated elements [duplicate]

This question already has answers here:
How can I find out the position of an item contained by an array?
(3 answers)
Closed 9 years ago.
Let's say I have the following array:
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
I want to find the index of the first "b" in the array. What is the best way of doing it?
Use Array#index
for first occurrence and Array#rindex for last occurrence of an element.
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
array.index("b") # => 6
array.rindex("b") # => 11

Resources