Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ SingleOrDefault() equivalent

In Typescript, I use this pattern often:

class Vegetable {     constructor(public id: number, public name: string) {     } }  var vegetable_array = new Array<Vegetable>(); vegetable_array.push(new Vegetable(1, "Carrot")); vegetable_array.push(new Vegetable(2, "Bean")); vegetable_array.push(new Vegetable(3, "Peas"));  var id = 1; var collection = vegetable_array.filter( xvegetable => {     return xvegetable.id == id; }); var item = collection.length < 1 ? null : collection[0];  console.info( item.name ); 

I am thinking about creating a JavaScript extension similar to the LINQ SingleOrDefault method where it returns null if it's not in the array:

var item = vegetable.singleOrDefault( xvegetable => {     return xvegetable.id == id}); 

My question is whether there is another way to achieve this without creating a custom interface?

like image 632
howardlo Avatar asked Oct 11 '15 13:10

howardlo


People also ask

What is SingleOrDefault in Linq?

SingleOrDefault<TSource>(IEnumerable<TSource>, TSource)Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

Which is better SingleOrDefault or FirstOrDefault?

When you want a default value is returned if the result set contains no record, use SingleOrDefault. When you always want one record no matter what the result set contains, use First or FirstOrDefault. When you want a default value if the result set contains no record, use FirstOrDefault.

What is difference between FirstOrDefault and SingleOrDefault in Linq?

FirstOrDefault() - Same as First(), but not thrown any exception or return null when there is no result. Single() asserts that one and only one element exists in the sequence. First() simply gives you the first one.

What is difference between single and SingleOrDefault in Linq?

Single : It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection. SingleOrDefault: It returns a single specific element from a collection of elements if element match found.


1 Answers

You can always use Array.prototype.filter in this way:

var arr = [1,2,3]; var notFoundItem = arr.filter(id => id === 4)[0]; // will return undefined var foundItem = arr.filter(id => id === 3)[0]; // will return 3 

Edit
My answer applies to FirstOrDefault and not to SingleOrDefault.
SingleOrDefault checks if there is only one match and in my case (and in your code) you return the first match without checking for another match.

BTW,If you wanted to achieve SingleOrDefault then you would need to change this:

var item = collection.length < 1 ? null : collection[0]; 

into

if(collection.length > 1)    throw "Not single result....";  return collection.length === 0 ? null : collection[0]; 
like image 58
Amir Popovich Avatar answered Sep 22 '22 17:09

Amir Popovich