【问题标题】:Retrieve variable from LINQ query从 LINQ 查询中检索变量
【发布时间】:2013-12-30 03:10:38
【问题描述】:

我在尝试从 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 =&gt; x.name == gr.Cells[1].Text).Any()){},如果名称匹配,我将复选框标记为选中。
同时,我需要根据数据库中的productName 来打包unitQuantity。但我不知道如何从 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 语句

【问题讨论】:

  • 你并没有真正在任何地方使用 b,一些额外的解释也可能有用:)
  • Product表和SPUItem表有关系吗?
  • SPUItem 通过 productVariantID 链接到 ProductVariant,ProductVariant 通过 productID 链接到 Product
  • 非常感谢。我已经解决了这个问题,尽管我认为我的解决方案不是最好的解决方案。

标签: c# asp.net linq gridview


【解决方案1】:

如果我没有误解您的实际需求,您可以尝试将 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;
}

【讨论】:

  • 我有另一个解决方案作为编辑部分。首先,我通过指定列来获取名称,然后按名称执行一些 sql 语句。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
相关资源
最近更新 更多