Asp.net/C#:将客户 ID 传递给函数中的 Linq 查询
Asp.net/C# :Passing Customer id to Linq query in a function
我正在尝试创建嵌套中继器。
我明白了我的意思,根据本文中的指南正确创建代码
我做到了,但是将客户 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();
return LINQ 查询的函数语法是什么。
根据给定的 id 创建 return 项的方法,创建 class 表示您想要 return
的数据项
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();
}
}
我正在尝试创建嵌套中继器。
我明白了我的意思,根据本文中的指南正确创建代码
我做到了,但是将客户 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();
return LINQ 查询的函数语法是什么。
根据给定的 id 创建 return 项的方法,创建 class 表示您想要 return
的数据项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();
}
}