简体   繁体   English

Razor 模板中的“序列不包含匹配元素”

[英]“Sequence contains no matching element” in Razor Template

I'm trying to fix a web page that was working, but now isn't.我正在尝试修复 web 页面,该页面正在运行,但现在不行。

在此处输入图像描述

In the image, it can be seen that feiEvt.DefiniteEntriesDate exists and has a date value.在图像中,可以看到feiEvt.DefiniteEntriesDate存在并且有一个日期值。

However, when the code reaches line 95, the closing } it fails with the exception Sequence contains no matching element但是,当代码到达第 95 行时,结束}它失败并出现异常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.如果我注释掉第 91 到 96 行,那么代码会在第 90 行失败,并带有feiEvt.EventTypeCode属性,但它也有数据。 When the 'Definite Entries` section is there, execution passes this line.当存在“Definite Entries”部分时,执行会通过这一行。

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!编辑:我还在屏幕截图中注释掉了第 90 行,现在第 89 行出现了同样的错误。这是引用不同的 object,但这个值@evt.ClassType也有一个值 - null 绝对不是问题或缺失值!

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.我尝试将逻辑分成单独的行 - 获取日期,检查它是否是 null,如果不是,将其转换为“ShortDateString”并显示字符串 - 但错误仍然相同。

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.问题是,@wiktorZychla 与“First()”的使用有关,但还有更多。 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.我更改了“FirstOrDefault”的代码,但仍然出错。 The actual error, the actual 'sequence with no matching element' was ' show.Events ' - it was an empty List.实际的错误,实际的“没有匹配元素的序列”是“ show.Events ”——它是一个空列表。

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.我的解决方案是在尝试访问之前检查show.Events列表中的值。

解决方案代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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