简体   繁体   中英

“Sequence contains no matching element” in Razor Template

I'm trying to fix a web page that was working, but now isn't.

在此处输入图像描述

In the image, it can be seen that feiEvt.DefiniteEntriesDate exists and has a date value.

However, when the code reaches line 95, the closing } it fails with the exception Sequence contains no matching element

If I comment out lines 91 to 96, then the code instead fails at line 90 with the feiEvt.EventTypeCode property, but that too has data. When the 'Definite Entries` section is there, execution passes this line.

Edit: I've also commented out line 90 in the screenshot, and the same error now occures on line 89. This is referencing a different object, but this value @evt.ClassType also has a value - it's definitely not an issue with null or missing values!

I've tried breaking the logic out into separate lines - get the date, check if it's null and if not, convert it into a 'ShortDateString' and display the string - but the error remains the same.

I've also tried (several times,) to Clean and then Build the entire app (the usual solution to such oddities) but again. no success.

Any ideas?

    @using System
@using System.Activities.Statements
@using System.Collections.Generic
@using System.Linq
@using BeSfClient.DBModels;
@using FEIClient.Data
@using FEIClient.Entry
@using FEIEntryWeb.Helpers
@using SharedUtilities
@model FEIEntryWeb.Models.EntriesModel
@{
    ViewBag.Title = "Entries";
}

@section Scripts {
    <script src="@Scripts.Url("~/scripts/entries.js")"></script>
}

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            @Html.ActionImage("_Entries", "Help",
                new { }, "~/Content/Images/help.png", "Members Help",
                new { @class = "helpLink" })
            <div class="title">
                <h1>@ViewBag.Title - @Model.Season</h1>
                <h2>@ViewBag.Message</h2>
            </div>

        </div>
    </section>
}

@using (Html.BeginForm("Entries", "Events", FormMethod.Post))
{
    @Html.DropDownListFor(i => i.BEMeetings, new SelectList(Model.BEMeetings.OrderBy(m => m.MeetingStartDate), "eventId", "MeetingName"))
    @*<input type="submit" name="action" value="Get Entries" />*@
    <button name="actionButton" value="GetEntries">Get Entries</button>

    @Html.CheckBoxFor(m => m.ShowAll, new { id = "showAll" })
    @Html.LabelFor(m => m.ShowAll, "Show All Events", new { style = "display:inline-block; padding-left: 10px" })


    if (Model.BEMeetings.Count > 0 && !string.IsNullOrWhiteSpace(Model.SelectedMeeting))
    {
        var meeting = Model.BEMeetings.First(m => m.EventId == Model.SelectedMeeting);
        var show = Model.FeiShow;

        <h3>@meeting.MeetingName - @Html.NullableDate(meeting.MeetingStartDate, "dd MMMM, yyyy")</h3>
        <ul>
            <li>Meeting ID: @meeting.EventId</li>
            <li>Venue: @meeting.VenueName</li>
            <li>FEI Show Code: @meeting.FeiShowCode</li>
        </ul>
        <div id="classesAccordion">

            @foreach (var evt in meeting.Classes)
            {
                // Get the entries for this class from the FEI Entries
                EntryListWS feiEvtEntries;
                Event feiEvt;
                List<BEEntry> beClass;
                Dictionary<string, Tuple<BEEntry, EntryAthleteWS>> matchedEntries;

                if (!Model.OrphanedFeiEntries.TryGetValue(evt.ClassId, out feiEvtEntries))
                {
                    feiEvtEntries = new EntryListWS();
                    feiEvtEntries.count = 0;
                    feiEvtEntries.entries = new EntryAthleteWS[] { };
                }

                if (!Model.OrphanedBeEntries.TryGetValue(evt.ClassId, out beClass))
                {
                    beClass = new List<BEEntry>();
                }

                if (!Model.MatchedEntries.TryGetValue(evt.ClassId, out matchedEntries))
                {
                    matchedEntries = new Dictionary<string, Tuple<BEEntry, EntryAthleteWS>>();
                }

                feiEvt = Model.FeiEvents[evt.ClassId];

                <h4>@evt.Description</h4>
                <div>
                    <ul>
                        <li>BE Class ID: @evt.ClassId</li>
                        <li>FEI Event ID: @evt.FeiEventCode</li>
                        <li>BE Event Type: @evt.ClassType</li>
                        <li>FEI Event Type: @feiEvt.EventTypeCode</li>
                        <li>
                            Definite Entries Date: @if (feiEvt.DefiniteEntriesDate != null)
                            {
                                <span>@(((DateTime)feiEvt.DefiniteEntriesDate).ToShortDateString())</span>
                            }
                        </li>
                        <li>
                            Open for Entry? <img src="~/Images/icons/fugue/icons/@((show.Events.First(e => e.EventCode == evt.FeiEventCode).OpenForEntries ?? false) ? "tick.png" : "cross.png")"
                                                 class="tooltip" title="Event Open For Entry?" />
                        </li>
                        <li>
                            Event History:
                            @{
                                var successResults = new[] { Results.SUCCESS, Results.OKD, Results.OKS };

                                if (evt.EventActions.Any(a => successResults.Contains(a.ActionResult)))
                                {
                                    @Html.ActionImage("ClassDetailsHistory",
                                        new { classId = evt.ClassId },
                                        "~/images/icons/aha/info.png", "Validated successfully",
                                        new { @class = "actionDetails" })
                                }
                                else if (evt.EventActions.Any())
                                {
                                    @Html.ActionImage("ClassDetailsHistory",
                                        new { classId = evt.ClassId },
                                        "~/images/icons/aha/warning.png", "Validated with errors",
                                        new { @class = "actionDetails" })
                                }
                                else
                                {
                                    @:No history recorded for this event
                                }
                            }
                        </li>
                    </ul>

                    <div class="entriesList">
                        <h4>Matched Entries</h4>

                        @if (matchedEntries.Count == 0)
                        {
                            <p>No Riders or Horses from British Eventing have been entered into this event yet.</p>
                        }
                        else
                        {
                            <div>
                                <button name="actionButton" value="ValidateList_@evt.ClassId" style="float:right">Validate Entry List</button>

                                <img src="~/Images/icons/aha/Filter.png" />:&nbsp;
                                <span class="customFilter gbrOnly" id="MatchedGbrOnly_@evt.ClassId"><img src="~/Images/icons/flags/GB.png" class="tooltip" title="GBR Only" /></span> |
                                <span class="customFilter notGbr" id="MatchedNotGbr_@evt.ClassId"><img src="~/Images/icons/flags/Not-GB.png" class="tooltip" title="Not GBR" /></span> |
                                <span class="customFilter allResults" id="MatchedAll_@evt.ClassId"><img src="~/Images/icons/fugue/icons/globe-green.png" class="tooltip" title="Everyone" /></span>

                                <table id="matchedEntriesTable_@evt.ClassId" class="entries dataTable">
                                    <thead>
                                        <tr>
                                            <th>Rider Name</th>
                                            <th>Rider FEI</th>
                                            <th>NF</th>
                                            <th>Horse Name</th>
                                            <th>Horse FEI</th>
                                            <th>Fei Status</th>
                                            <th class="enter">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var entry in matchedEntries)
                                        {
                                            var beEntry = entry.Value.Item1;
                                            var feiEntry = entry.Value.Item2;
                                            var feiHorse = feiEntry.horses.FirstOrDefault(h => h.fei_id.Trim().Equals(beEntry.HorseFEI.Trim(), StringComparison.InvariantCultureIgnoreCase));

                                            var athleteNamesMatches = Model.CheckBeAndFeiAthleteNamesMatch(beEntry, feiEntry);
                                            var horseNameMatches = Model.CheckBeAndFeiHorseNamesMatch(beEntry, feiHorse);

                                            <tr>
                                                <td @if (!athleteNamesMatches) { @: class="error"
                                                    }>
                                                    @if (!athleteNamesMatches)
                                                    {
                                                        <span class="tooltip" title="@String.Format("{0} {1}", feiEntry.firstname, feiEntry.familyname)">@beEntry.RiderFirstName @beEntry.RiderFamilyName</span>
                                                        <!-- FEI: @feiEntry.firstname @feiEntry.familyname BE: @beEntry.RiderFirstName @beEntry.RiderFamilyName -->
                                                    }
                                                    else
                                                    {
                                                        @String.Format("{0} {1}", beEntry.RiderFirstName, beEntry.RiderName)
                                                    }
                                                </td>
                                                <td>
                                                   @Html.SalesforceLink(beEntry.RiderId, beEntry.RiderFEI)
                                                </td>

                                                <td>@beEntry.RiderNationality</td>
                                                <td @if (!horseNameMatches) { @: class="error"
                                                    }>
                                                    @if (!horseNameMatches)
                                                    {
                                                        <span class="tooltip" title="@feiHorse.name">@beEntry.HorseName</span>
                                                    }
                                                    else
                                                    {
                                                        @beEntry.HorseName
                                                    }
                                                </td>
                                                <td>
                                                    @Html.SalesforceLink(beEntry.HorseId, beEntry.HorseFEI)
                                                </td>

                                                <td>@feiEntry.status</td>
                                                <td class="enter">
                                                    <img src="~/images/icons/silk/delete.png" alt="Withdraw Entry"
                                                         id="Withdraw_@(beEntry.ClassId + "_" + @beEntry.EntryId)"
                                                         class="action" />
                                                    @if (beEntry.EntryActions.Any(a => a.ActionResult != Results.SUCCESS && a.ActionResult != Results.ATH_ALREADY_ENTERED))
                                                    {
                                                        @Html.ActionImage("EntryDetailsHistory",
                                                            new { classId = beEntry.ClassId, entryId = beEntry.EntryId },
                                                            "~/images/icons/aha/warning.png", "Entered with errors",
                                                            new { @class = "actionDetails tooltip" })
                                                        ;
                                                    }
                                                    else if (beEntry.EntryActions.Any())
                                                    {
                                                        @Html.ActionImage("EntryDetailsHistory",
                                                            new { classId = beEntry.ClassId, entryId = beEntry.EntryId },
                                                            "~/images/icons/aha/info.png", "Entered successfully",
                                                                 new { @class = "actionDetails tooltip" })
                                                        ;
                                                    }
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            </div>
                        }

                        <h4>BE Entries</h4>
                        @if (beClass.Count > 0)
                        {
                            <div>
                                @*<input type="submit" value="Enter Selected Riders/Horses" name="ActionEnter_@evt.ClassId" style="float:right" />*@
                                <img src="~/Images/icons/aha/Filter.png" />:&nbsp;

                                <span class="customFilter gbrOnly" id="BeGbrOnly_@evt.ClassId"><img src="~/Images/icons/flags/GB.png" class="tooltip" title="GBR Only" /></span> |
                                <span class="customFilter notGbr" id="BeNotGbr_@evt.ClassId"><img src="~/Images/icons/flags/Not-GB.png" class="tooltip" title="Not GBR" /></span> |
                                <span class="customFilter validOnly" id="BeValidOnly_@evt.ClassId"><img src="~/Images/icons/fugue/icons/tick-button.png" class="tooltip" title="Valid Entries Only" /></span> |
                                <span class="customFilter feiPerson" id="BeFeiPerson_@evt.ClassId"><img src="~/Images/icons/misc/PersonInvalid.png" class="tooltip" title="Rider FEI Membership Issues" /></span> |
                                <span class="customFilter feiHorse" id="BeFeiHorse_@evt.ClassId"><img src="~/Images/icons/misc/HorseInvalid.png" class="tooltip" title="Horse FEI Membership Issues" /></span> |
                                <span class="customFilter allResults" id="BeAll_@evt.ClassId"><img src="~/Images/icons/fugue/icons/globe-green.png" class="tooltip" title="Everyone" /></span>

                                <button name="actionButton" value="ActionEnter_@evt.ClassId" style="float: right">Enter Selected Riders/Horses</button>

                                <table id="beEntriesTable_@evt.ClassId" class="entries dataTable">
                                    <thead>
                                        <tr>
                                            <th>Rider Name</th>
                                            <th>Rider FEI</th>
                                            <th>NF</th>
                                            <th>Horse Name</th>
                                            <th>Horse FEI</th>
                                            <th>BE Entry ID</th>
                                            <th class="enter">Enter<br />@Html.CheckBox("Enter_All_" + @evt.ClassId)</th>
                                        </tr>
                                    </thead>

                                    <tbody>
                                        @foreach (var entry in beClass)
                                        {
                                            var validForEntry = (!String.IsNullOrWhiteSpace(entry.RiderFEI)
                                                                 && !String.IsNullOrWhiteSpace(entry.HorseFEI)
                                                                 && !String.IsNullOrWhiteSpace(entry.RiderNationality)
                                                                 && entry.RiderNationality.Trim().Equals("GBR", StringComparison.InvariantCultureIgnoreCase)
                                                                 && entry.RiderFeiValid
                                                                 && entry.HorseFeiValid) ? "valid" : "";
                                            var feiAthleteMembership = (entry.RiderFeiValid) ? "" : "invalid";
                                            var feiHorseMembership = (entry.HorseFeiValid) ? "" : "invalid";
                                            <tr>
                                                <td>
                                                    @entry.RiderFirstName, @entry.RiderName
                                                </td>
                                                <td data-search="@feiAthleteMembership">
                                                    @if (!entry.RiderFeiValid)
                                                    {
                                                        <img src="~/images/icons/fugue/icons/cross-small.png" style="float: right; clear: right"
                                                             class="tooltip" title="No current FEI Membership" />
                                                    }
                                                    @Html.SalesforceLink(entry.RiderId, entry.RiderFEI)
                                                </td>
                                                <td>@entry.RiderNationality</td>
                                                <td>
                                                    @entry.HorseName
                                                </td>
                                                <td data-search="@feiHorseMembership" style="vertical-align: middle">
                                                    @if (!entry.HorseFeiValid)
                                                    {
                                                        <img src="~/images/icons/fugue/icons/cross-small.png" style="float: right"
                                                             class="tooltip" title="No current FEI Membership" />
                                                    }
                                                    @Html.SalesforceLink(entry.HorseId, entry.HorseFEI)
                                                </td>
                                                <td>@entry.EntryId</td>
                                                <td class="enter" data-search="@validForEntry">
                                                    @if (!String.IsNullOrWhiteSpace(validForEntry))
                                                    {
                                                        @Html.CheckBoxSimple("Enter_" + @evt.ClassId + "_" + entry.EntryId, new { })
                                                    }
                                                    @if (entry.EntryActions.Any(a => a.ActionResult != Results.SUCCESS && a.ActionResult != Results.ATH_ALREADY_ENTERED))
                                                    {
                                                        @Html.ActionImage("EntryDetailsHistory",
                                                            new { classId = entry.ClassId, entryId = entry.EntryId },
                                                            "~/images/icons/aha/warning.png", "Entered with errors",
                                                            new { @class = "actionDetails" })
                                                    }
                                                    else if (entry.EntryActions.Any())
                                                    {
                                                        @Html.ActionImage("EntryDetailsHistory",
                                                            new { classId = entry.ClassId, entryId = entry.EntryId },
                                                            "~/images/icons/aha/info.png", "Entered successfully",
                                                            new { @class = "actionDetails" })
                                                    }
                                                    @Html.ActionImage("EditEntry",
                                                            new { classId = entry.ClassId, entryId = entry.EntryId },
                                                            "~/images/icons/silk/cog_edit.png", "Edit FEI Details",
                                                         new { @class = "actionDetails tooltip" })
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>

                            </div>
                        }
                        else
                        {
                            <p>No BE Entries in database, or all have been entered into FEI Database (see Matched Entries above)</p>
                        }

                        <h4>FEI Entries</h4>
                        @if (feiEvtEntries.entries.Length > 0)
                        {
                            if (feiEvtEntries.entries.Length == 1 && feiEvtEntries.entries[0].fei_id == 0)
                            {
                                <p>Event not open for entries.</p>
                            }
                            else
                            {
                                <div>
                                    <img src="~/Images/icons/aha/Filter.png" />:&nbsp;
                                    <span class="customFilter gbrOnly" id="FeiGbrOnly_@evt.ClassId"><img src="~/Images/icons/flags/GB.png" class="tooltip" title="GBR Only" /></span> |
                                    <span class="customFilter notGbr" id="FeiNotGbr_@evt.ClassId"><img src="~/Images/icons/flags/Not-GB.png" class="tooltip" title="Not GBR" /></span> |
                                    <span class="customFilter allResults" id="FeiAll_@evt.ClassId"><img src="~/Images/icons/fugue/icons/globe-green.png" class="tooltip" title="Everyone" /></span>
                                    <table id="feiEntriesTable_@evt.ClassId" class="entries dataTable">
                                        <thead>
                                            <tr>
                                                <th>Rider Name</th>
                                                <th>Rider FEI</th>
                                                <th>NF</th>
                                                <th>Horse Name</th>
                                                <th>Horse FEI</th>
                                                <th>Fei Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach (var entry in feiEvtEntries.entries.ToList())
                                            {
                                                foreach (var horse in entry.horses.ToList())
                                                {
                                                    <tr>
                                                        <td>@entry.firstname @entry.familyname</td>
                                                        <td>@entry.fei_id</td>
                                                        <td>@entry.competing_for_nf</td>
                                                        <td>@horse.name</td>
                                                        <td>@horse.fei_id</td>
                                                        <td>@horse.status</td>
                                                    </tr>
                                                }
                                            }
                                        </tbody>
                                    </table>
                                </div>
                            }
                        }
                        else
                        {
                            <p>No entries recorded with FEI yet</p>
                        }
                    </div>
                </div>
            }
        </div>
    }
    else
    {
        <p>Select a Meeting to view its Entries</p>
    }
}

A bit late perhaps, but I found the solution, so posting here for anyone else who has this problem.

The issue was, as @wiktorZychla related to the use of 'First()' but there was more to it. This is the original code:

                <li>
                    Definite Entries Date: @if (feiEvt.DefiniteEntriesDate != null)
                    {
                        <span>@(((DateTime)feiEvt.DefiniteEntriesDate).ToShortDateString())</span>
                    }
                </li>
                <li>
                    Open for Entry? <img src="~/Images/icons/fugue/icons/@((show.Events.First(e => e.EventCode == evt.FeiEventCode).OpenForEntries ?? false) ? "tick.png" : "cross.png")"
                                         class="tooltip" title="Event Open For Entry?" />
                </li> 

Although the exception was reported in the statement

@(((DateTime)feiEvt.DefiniteEntriesDate).ToShortDateString())

The exception was actually happening in this line:

@((show.Events.First(e => e.EventCode == evt.FeiEventCode).OpenForEntries ?? false) ? "tick.png" : "cross.png")

I changed the code for 'FirstOrDefault' but it STILL errored. The actual error, the actual 'sequence with no matching element' was ' show.Events ' - it was an empty List.

The thing that made this hard to figure out was the the exception was occuring (despite plenty of clean/rebuild, addition of blank lines and removing some lines) on the line AFTER the line the Exception was reported on.

My solution was to check the show.Events List for values before trying to access it.

解决方案代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM