ASP.Net MVC Error: Sequence contains no elements

Last Reply one year ago By dharmendr

Posted one year ago

Hello everyone,

I had my view displaying everything in the list than I changed it to a new set or properties My list is populated but I am getting the error Sequence contains no elements 

Line 212:                                if (Model.Settings.Show[j])

Line 213:                                {

Line 214:                                    var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();

Line 215:                                    <td style="padding-right:20px;">@quote.GetValue(current,null)</td>

Line 216:                                }

I did exactly what I did before but for some reason im not getting the same results. Here is my code that I am working with the before and after.

@* Version >= 2.0.6  AFTER   *@

                    @for(int i=0; i<Model.ListQuotes.Count; i++)
                    {
                        var current = Model.ListQuotes[i];
                        <tr>
                            <td><input type="checkbox" class="checkbox" name="@(i + " Rower")" id="@(i + " Rower")"/></td>
                            @for(int j=0; j<Model.Settings.Columns.Length; j++)
                            {
                                if (Model.Settings.Show[j])
                                {
                                    var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                    <td style="padding-right:20px;">@quote.GetValue(current,null)</td>
                                }
                                else
                                {
                                    var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                    <td style="padding-right:20px; display:none;">@quote.GetValue(current,null)</td>
                                }
                               
                            }
                            <td><input type="button" class="btn btn-danger" onclick="modify(this.parentElement.parentElement)" value="Edit"/></td>
                        </tr>
                    }

@* Version <= 2.0.5  BEFOREEEE *@

                    @*@for (int i=0; i<Model.List.Count; i++)
                    {
                        var current = Model.List[i];
                        <tr>
                            <td><input type="checkbox" class="checkbox" name="@(i + " Rower")" id="@(i + " Rower")" /></td>
                            @for (int j=0; j<Model.Settings.Columns.Length; j++)
                            {
                                if (Model.Settings.Show[j])
                                {
                                    var quo = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                    <td style="padding-right:20px;">@quo.GetValue(current, null)</td>
                                }
                                else
                                {
                                    var quo = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                    <td style="padding-right:20px; display:none;">@quo.GetValue(current, null)</td>
                                }
                            }
                            <td><input type="button" class="btn btn-danger" onclick="modify(this.parentElement.parentElement)" value="Edit" /></td>
                        </tr>
                    }*@ /// Setting up ServiceQuote methods 

 

Posted one year ago

Hi bigbear,

When you get the LINQ error Sequence contains no elements, this is usually because you are using the First() or Single() command rather than FirstOrDefault() and SingleOrDefault().

bigbear says:
if (Model.Settings.Show[j])
                            {
                                var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                <td style="padding-right:20px;">@quote.GetValue(current,null)</td>
                            }
                            else
                            {
                                var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).First();
                                <td style="padding-right:20px; display:none;">@quote.GetValue(current,null)</td>
                            }

So change above line with below.

if (Model.Settings.Show[j])
{
    var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).FirstOrDefault();
    <td style="padding-right:20px;">@quote.GetValue(current,null)</td>
}
else
{
    var quote = current.GetType().GetProperties().Where(x => x.Name.Equals(Model.Settings.Columns[j])).FirstOrDefault();
    <td style="padding-right:20px; display:none;">@quote.GetValue(current,null)</td>
}