Asp.net/C#:将客户id传递给函数中的Linq查询

本文关键字:函数 查询 Linq id net 客户 Asp | 更新日期: 2023-09-27 18:17:00

我正在尝试创建一个嵌套中继器。

我明白了我的意思,在这篇文章中通过这个指南正确地创建代码

http://www.aspsnippets.com/Articles/Implement-Nested-Repeater-Repeater-inside-Repeater-with-example-in-ASPNet-using-C-and-VBNet.aspx

我做到了,但是将客户id传递给函数有一个问题。

我会用代码来解释,我的。aspx代码:

<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="rptCustomers_ItemDataBound">
<HeaderTemplate>
    <table class="table table-hover table-striped table-condensed table-bordered table-responsive" >
        <tr>
            <th>
            </th>
            <th>
                Deposit Number 
            </th>
            <th>
                Custom Declaration
            </th>
              <th>Category</th>
                    <th>Location</th>
                    <th>Goods Description</th>
                    <th>Units Balance</th>
                    <th>WT Balance</th>
                    <th>Goods Balance Amount(LC)</th>
        </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td>
            <img alt="" style="cursor: pointer" src="images/plus.png" />
            <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                <asp:Repeater ID="rptOrders" runat="server">
                    <HeaderTemplate>
                        <table class="table table-hover table-striped table-condensed table-bordered table-responsive" >
                            <tr>
                                <th>
                                    Item No
                                </th>
                                <th>
                                    Item descr
                                </th>
                                <th>
                                    UOM
                                </th>
                                  <th>
                                    Balance Units
                                </th>
                                  <th>
                                    Balance LC
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                             <%# Eval("itemNo") %>
                            </td>
                            <td>
                            <%# Eval("itemDesc") %> 
                            </td>
                            <td>
                            <%# Eval("Uom") %> 
                            </td>
                             <td>
                            <%# Eval("balUnits") %> 
                            </td>
                            <td>
                            <%# Eval("balLc") %> 
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </asp:Panel>
            <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("depID") %>' />
        </td>
        <td>
            <%# Eval("depNo") %>
        </td>
        <td>
            <%# Eval("customDec") %> 
        </td>
          <td><%#Eval("category") %></td>
                   <td><%#Eval("location") %></td>
                   <td><%#Eval("goodDesc") %></td>
                   <td><%#Eval("unitsBal") %></td>
                   <td><%#Eval("wtBal") %></td>
                   <td><%#Eval("lcBal") %></td>  

    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>

现在这是执行客户id的隐藏字段:

 <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("depID") %>' />

这是page_load:

中父中继器的代码
protected void Page_Load(object sender, EventArgs e)
{
    var query = (from cd in db.CDIndexes
                 join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId
                 join ter in db.Territories on cd.cdin_Secterr equals ter.Terr_TerritoryID
                 where cd.cdin_Deleted == null &&
                 com.Comp_Deleted == null &&
                 cd.cdin_Status == "InProgress" &&
                 com.Comp_CompanyId == 408
                 select new
                 {
                     depID=cd.cdin_CDIndexID,
                     location = ter.Terr_Caption,
                     depNo = cd.cdin_Serial,
                     customDec = cd.cdin_Customdeclar,
                     category = cd.cdin_category,
                     goodDesc = cd.cdin_goodsDesc,
                     unitsBal = cd.cdin_RemainPackages,
                     wtBal = cd.cdin_RemainWT,
                     lcBal = cd.cdin_ActMortgageAmnt,
                 }
               ).ToList();

    rptCustomers.DataSource = query;
    rptCustomers.DataBind();
}

这是父中继器的ItemDataBound

protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var queryItems = (from cd in  db.CDIndexes  
                      join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId
                      join terr in db.Territories on cd.cdin_Secterr equals terr.Terr_TerritoryID
                      join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId
                      join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID
                      join capt in db.Custom_Captions on gds.good_UOM equals capt.Capt_Code
                      where 
                        capt.Capt_Family== "good_UOM" &&
                        cd.cdin_Deleted == null &&
                        cd.cdin_Status== "InProgress" &&
                        cd.cdin_CompanyId==408 &&
                        cd.cdin_CDIndexID== 2506542 &&
                        itms.item_Deleted == null &&
                        cd.cdin_GoodsProperty=="01" &&
                        com.Comp_Deleted== null
                        select new
                        {
                            itemNo=itms.item_itemNo,
                            itemDesc=itms.item_Name,
                            Uom=capt.Capt_US,
                            balUnits=gds.good_RemainWT,
                            balLc=gds.good_BalanceAmtLC,
                        }
                      ).ToList();
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
        Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
        rptOrders.DataSource = queryItems;
        rptOrders.DataBind();
    }
}

我想要的是如何将客户ID值传递给LINQ查询,特别是在以下语句中传递ID而不是数字(2506542):

cd.cdin_CDIndexID== 2506542

所以我需要在以下代码中替换中继器的数据源:

  rptOrders.DataSource = queryItems;
            rptOrders.DataBind();
通过带参数string customerId值的函数调用

例如:

rptOrders.DataSource = function(customerId);
                rptOrders.DataBind();

返回LINQ查询的函数的语法是什么

Asp.net/C#:将客户id传递给函数中的Linq查询

创建一个基于给定id返回项的方法,创建一个代表您想要返回的数据项的类

public class DataItem
{
    public string ItemNo { get; set; }
    public string ItemDesc { get; set; }
    public string Uom { get; set; }
    public string BalUnits { get; set; }
    public string BalLc { get; set; }
}
private IEnumerable<DataItem> GetItemsById(int customerId)
{
    var queryItems = 
        from cd in  db.CDIndexes  
            join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId
            join terr in db.Territories on cd.cdin_Secterr equals terr.Terr_TerritoryID
            join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId
            join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID
            join capt in db.Custom_Captions on gds.good_UOM equals capt.Capt_Code
        where 
            capt.Capt_Family== "good_UOM" &&
            cd.cdin_Deleted == null &&
            cd.cdin_Status== "InProgress" &&
            cd.cdin_CompanyId==408 &&
            cd.cdin_CDIndexID== customerId && // Just use parameter instead of number
            itms.item_Deleted == null &&
            cd.cdin_GoodsProperty=="01" &&
            com.Comp_Deleted== null
        select new DataItem
               {
                   ItemNo=itms.item_itemNo,
                   ItemDesc=itms.item_Name,
                   Uom=capt.Capt_US,
                   BalUnits=gds.good_RemainWT,
                   BalLc=gds.good_BalanceAmtLC,
               };        
}

然后在您的RowDataBound方法中使用此方法

protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string fieldValue= (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
        int customerId = int.Parse(fieldValue);
        Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
        rptOrders.DataSource = GetItemsById(customerId).ToList();
        rptOrders.DataBind();
    }
}