即使在SQL

时间:2018-10-02 19:26:11

标签: c# asp.net sql-server listview duplicates

问题:只要有相同的“计划”,SQL中的行之一就会被复制到网页上

我尝试检查该语句的Where和OrderBy,以查看这是否影响重复的原因。我不确定为什么OrderBy会创建重复项。我已经尝试过使用多个用户,并且当他们具有相同的计划时,行为是一致的。

示例如下:

SQL vs ASPX Page More Examples

代码(.cs):

var someVariable = DbContext.DbSet<>.Where(
                    m =>
                        m.Some_ID == CurrentlyEditingSomeID
                    ).AsQueryable().OrderByDescending(m => m.Start_Date); 

这引用了.aspx页中的ListView,其中包含以下各项:

   <td style="width: 20%;"><%# Item.Plan %></td>
                <td style="width: 20%;"><%# Item.Start_Date.ToSafeShortDate() %></td>
                <td style="width: 20%;"><%# Item.End_Date.ToSafeShortDate() %></td>

LINQ查询中的输出:

SELECT 
[Project1].[Field1] AS [Field1], 
[Project1].[Field2] AS [Field2], 

FROM ( SELECT 
    [Extent1].[Field1] AS [Field1], 
    [Extent1].[Field2] AS [Field2], 

    FROM (SELECT 
[VIEW].[Field1] AS [Field1], 
[VIEW].[Field2] AS [Field2], 

FROM [mWeb].[VIEW] AS [VIEW]) AS [Extent1]
    WHERE [Extent1].[Some_ID] = @p__linq__0
)  AS [Project1]
ORDER BY [Project1].[Start_Date] DESC

(。aspx文件的较长版本):

<asp:ListView ID="lstPlanE" runat="server" ItemPlaceholderID="litPlanEPlaceHolder"
        ItemType="Project.Data.view" SelectMethod="lstPlanE_GetData">
        <EmptyDataTemplate>
            <div class="alert alert-danger" role="alert">Nothing found</div>
        </EmptyDataTemplate>
        <LayoutTemplate>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th style="width: 20%;">G</th>
                            <th style="width: 20%;">G ID</th>
                            <th style="width: 20%;">Plan</th>
                            <th style="width: 20%;">Start Date</th>
                            <th style="width: 20%;">End Date</th>
                        </tr>
                    </thead>
                    <asp:Literal ID="litPlanEPlaceHolder" runat="server" />
                </table>
            </div>
        </LayoutTemplate>

            <ItemTemplate>
                <tr>
                    <td style="width: 20%;"><%# Item.G_Description %></td>

                       <% if (isAAA()) %> 
                       <% {  %>   
                                     <td><%# Item.AAA_G_ID %></td> 
                       <% } %>

                       <% else %>
                       <% { %>
                                 <td><%# Item.G_ID %></td>
                       <% } %>



                    <td style="width: 20%;"><%# Item.Plan %></td>
                    <td style="width: 20%;"><%# Item.Start_Date.ToSafeShortDate() %></td>
                    <td style="width: 20%;"><%# Item.End_Date.ToSafeShortDate() %></td>

                </tr>
            </ItemTemplate>
    </asp:ListView>

(。cs文件中方法的较长版本):

public IQueryable<MediView.Data.vx_EligibilitySearch> lstPlanE_GetData()
        {
            try
            {
                if (isAAA())
                {
                    var someVariable = DbContext.DbSet<>.Where(
                        m =>
                            m.Other_ID == CurrentlyEditingOtherID &&
                            m.Some_Seq == CurrentlyEditingSomeSeqID
                        ).AsQueryable();
                    return someVariable;
                }
                else
                {

                    var someVariable = DbContext.DbSet<>.Where(
                    m =>
                        m.Some_ID == CurrentlyEditingSomeID
                    ).AsQueryable().OrderByDescending(m => m.Start_Date); 

                    return someVariable;
                }

            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
            }
            return null;
        }

public bool isAAA() // Determines if Site is CCC or Other
    {
        bool isSiteCCC = false;
        String browserTab = Properties.Settings.Default.BrowserTab.Trim(); // Gets Site Name from Settings file

        if (browserTab.Equals("Some Site Name")) { isSiteCCC = true; }

        return isSiteAAA;
    }

1 个答案:

答案 0 :(得分:0)

我做出的两项更改已解决:在OrderBy中从“开始日期”更改为“结束日期”。 还将其更改为AsNoTracking。

经过测试,唯一真正起作用的是AsNoTracking()方法。

也许有人可以解释为什么这样做?

else
            {

                var someVariable = DbContext.DbSet<>.AsNoTracking().Where(
                m =>
                    m.Some_ID == CurrentlyEditingSomeID
                ).AsQueryable().OrderByDescending(m => m.End_Date); 

                return someVariable;
            }