locked
MVC List contains no elements but in Debugger has 212 elements. RRS feed

  • Question

  • User-544325736 posted

    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.

    public class ServiceModel
        {
            public string Scustomers { get; set; }
            public string SserialNumbers { get; set; }
            public string Scity { get; set; }
            public string Sstate { get; set; }
            public IEnumerable<SelectListItem> Customers { get; set; }
            public IEnumerable<SelectListItem> SerialNumbers { get; set; }
            public IEnumerable<SelectListItem> City { get; set; }
            public IEnumerable<SelectListItem> State { get; set; }
            //public List<Service_Report_Fields> List { get; set; }
            public TableSettings Settings { get; set; }
            public List<ServiceQuote> ListQuotes { get; set; }
        } public class TableSettings
        {
            public string[] Labels { get; set; }
            public bool[] Show { get; set; }
            public string[] Columns { get; set; }
        }
    @* 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 
            public TableSettings GetTableSettingsSQ()
            {
                TableSettings tableSettings = new TableSettings();
                var prop = (from sq in typeof(ServiceQuote).GetProperties()
                            select sq.Name).ToArray();
                var settings = (from userPs in context.DBDEF_UserPersonalizations
                                join colLab in context.DBDEF_ColumnLabels
                                on userPs.ColumnLabel_ID
                                equals colLab.ID
                                where userPs.UserID == 0 && userPs.FormName.Equals("ServiceQuote")
                                select new
                                {
                                    userPs.Sequence,
                                    userPs.IsVisible,
                                    colLab.ColumnLabel,
                                    colLab.ColumnName
                                }).ToList();
                tableSettings.Labels = new string[settings.Count];
                tableSettings.Show = new bool[settings.Count];
                tableSettings.Columns = new string[settings.Count];
    
                foreach(var item in settings)
                {
                    tableSettings.Labels[item.Sequence.Value] = item.ColumnLabel;
                    tableSettings.Show[item.Sequence.Value] = item.IsVisible.Value;
                    tableSettings.Columns[item.Sequence.Value] = item.ColumnName;
                }
                return tableSettings;
            }
            public List<ServiceQuote> GetrowsSQ(QueryInfo info)
            {
                string city = info.City;
                string state = info.State;
                string customer = info.Customer;
                int? serial = info.SerialNumber;
                bool? contract = info.New;
                bool? renewal = info.Renewal;
                bool? upcoming = info.Upcoming;
    
                var rows = (from SQ in context.ServiceQuotes
                            select SQ);
                if (!string.IsNullOrEmpty(info.City))
                {
                    rows = (from SQ in rows
                            where SQ.City.Equals(city)
                            select SQ);
                }
                if (!string.IsNullOrEmpty(state))
                {
                    rows = (from SQ in rows
                            where SQ.State.Equals(state)
                            select SQ);
                }
                if (!string.IsNullOrEmpty(customer))
                {
                    rows = (from SQ in rows
                            where SQ.CustomerID.Equals(customer)
                            select SQ);
                }
                //if(serial != null)
                //{
                //    int[]
                //    rows = (from SQ in rows
                //            where SQ.ListOfSN2s.Split(',').Select(int.Parse).ToList() == serial
                //            select SQ);
                //}
                if (renewal != null && renewal == true)
                {
                    rows = (from SQ in rows
                            where SQ.ExpirationDate > DateTime.Now && SQ.ExpirationDate < DateTime.Now.AddMonths(3)
                            select SQ);
                }
                if (upcoming != null && upcoming == true)
                {
                    rows = (from SQ in rows
                            where SQ.ExpirationDate > DateTime.Now && SQ.ExpirationDate < DateTime.Now.AddMonths(3)
                            select SQ);
                }
                //foreach (var row in rows)
                //{
                //    var q = row.GetType().GetProperties().Where(x => x.Name.Equals("QuoteID")).FirstOrDefault();
                //    string qq = q.ToString();
                //    var otherThing = q.GetValue(row, null);
                //}
                return rows.ToList();
            }
            
            public IEnumerable<SelectListItem> GetCustomersSQ()
            {
                var customers = (from SQ in context.ServiceQuotes
                                 select new SelectListItem
                                 {
                                     Text = SQ.CustomerID,
                                     Value = SQ.CustomerID
                                 }).Distinct().ToList();
                return customers;
            }
    

    Monday, September 23, 2019 4:43 PM

Answers

  • User61956409 posted

    Hi ExceedingLife,

    Sequence contains no elements

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

    I do a test with testing data and can reproduce same issue, if it is empty (contains no elements) and we still call .first() method.

    To troubleshoot the issue, please try to debug and trace Model.Settings.Columns[j] and quotes like below, and check if quotes is empty.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 24, 2019 7:23 AM