当试图从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()){},如果名称是匹配的,我会标记选中的checkBox。
同时,我需要从数据库中获取基于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语句。
发布于 2013-12-30 03:34:40
如果我没有误解您实际想要的是什么,可以尝试将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;
}https://stackoverflow.com/questions/20832772
复制相似问题