How to use SelectMany in typescript

62 Views Asked by At

I am getting this error and I am not sure why. I see that the this.dayscalendar.Days is a array with data. I am trying to use the SelectMany and select the one record that matches.

linq.js:1521 Uncaught Error: Single:No element satisfies the condition.

Enumerable.From(this.dayscalendar.Days)
    ArrayEnumerable {source: Array(7)}
    source
    : 
    Array(7)
    0: {Day: 'Sun', IsSelected: false}
    1: {Day: 'Mon', IsSelected: false}
    2: {Day: 'Tues', IsSelected: false}
    3: {Day: 'Wed', IsSelected: false}
    4: {Day: 'Thus', IsSelected: false}
    5: {Day: 'Fri', IsSelected: false}
    6: {Day: 'Sat', IsSelected: false}

Line getting the error

Enumerable.From(this.dayscalendar.Days).SelectMany(w => w.day).Single(d => d.day === 'Wed');
1

There are 1 best solutions below

2
Hassan Serhan On BEST ANSWER
Enumerable.From(this.dayscalendar.Days).Select(w => w.Day).Single(d => d === 'Wed');

If you want to get the whole object, you can apply Single directly as follows:

Enumerable.From(this.dayscalendar.Days).Single(d => d.Day === 'Wed');

This will select the Day property from each item in dayscalendar.Days, then find the one that equals 'Wed'.

Also note that Single() will throw an error if there isn't exactly one element that satisfies the condition.