尝试从LINQ查询中检索某些内容时遇到问题。我有一个prodList存储所有产品记录。另一个列表是distSPUItemList,它存储记录分布式项目记录。这是代码:
//Get list of products based on category and bind data to grid view
prodList = prodPackBLL.getAllProductByCategory(category);
gv.DataSource = prodList;
gv.DataBind();
//Mark checkbox checked for products included in standard packing list
var SPUItemList = new List<DistributionStandardPackingUnitItems>();
SPUItemList = packBLL.getAllSPUItemByDistributionID(distributionID);
//LINQ query to compare lists of objects finding ProductPacking
//objects whose name property are equal to the items in SPUItemList
var available = from a in prodList
// perform an inner-join between prodList and SPUItemList only
// (x => x.name == a.name) compares b.name to a.name, this is
// specifically what enforces that names match
from b in SPUItemList.Where(x => x.name == a.name)
// after inner joining is done, only select objects from prodList
select a;
//Iterate the gridview rows
GridView gvForCheckBox = (GridView)e.Item.FindControl("gvProduct") as GridView;
foreach (GridViewRow gr in gvForCheckBox.Rows)
{
if (available.Where(x => x.name == gr.Cells[1].Text).Any())
{
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
cb.Checked = true;
TextBox tb = (TextBox)gr.Cells[3].FindControl("tbQuantity");
//From here retrieve name and pass it to data access layer to perform SQL
//Display unitQuantity if name match. If not default text set to 0
}
}
所以基本上我要做的是from if (available.Where(x => x.name == gr.Cells[1].Text).Any()){}
,如果名称匹配,我选中复选框。
同时,我需要根据数据库中的unitQuantity
来打包productName
。但我不知道如何从LINQ查询中检索名称。
对不起我的不良解释并提前致谢。
编辑部分:
string name = gr.Cells[1].Text;
int productQuantity = packBLL.getProductQuantityByName(name);
TextBox tb = (TextBox)gr.Cells[3].FindControl("tbQuantity");
tb.Text = productQuantity.ToString();
适用于此。首先,我通过指定列获取名称,然后执行其他一些Sql语句
答案 0 :(得分:0)
如果我没有误解你的实际操作,可以尝试将foreach
循环内部更改为:
var rowData = available.Where(x => x.name == gr.Cells[1].Text).FirstOrDefault();
//if rowData == null, means no matching data in `available` variable
if (rowData != null)
{
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
cb.Checked = true;
TextBox tb = (TextBox)gr.Cells[3].FindControl("tbQuantity");
//example on how to get current name
var name = rowData.name;
}