将值从labelbox传递给linq查询

本文关键字:linq 查询 labelbox | 更新日期: 2023-09-27 18:17:25

大家好,我是linq的新手,所以是在。net。我想传递一个值从标签框到linq查询作为参数。下面是代码…

namespace PblCard.PublicWeb
{
public partial class Cardno : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        {
            if (!IsPostBack)
            {
                if (Page.User.IsInRole(SecurityEngine.Roles.Customer))
                {
                    Customer customer = new      CustomerEngine().GetCustomerByEmail(Page.User.Identity.Name);
                    if (customer == null)
                        throw new ApplicationException(Properties.Resources.CustomerNotFound);

                    lblCardNo.Text = customer.CardNumber;
                }
            }
        }
    }
    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
       string panno = lblCardNo.Text;
        using (Entities query = new Entities())
        {
            var txn = from p in query.TRANSACTIONs
                      .Where(x => x.PAN == panno)
                      select p;
            GridView1.DataSource = txn;
            GridView1.DataBind();
        }
    }
}
}

但是当绑定到网格时,我没有得到任何输出。但是如果我用这个

protected void ASPxButton2_Click(object sender, EventArgs e)
    {
       string panno = lblCardNo.Text;
        using (Entities query = new Entities())
        {
            var txn = from p in query.TRANSACTIONs
                      .Where(x => x.PAN == ("234567"))
                      select p;
            GridView1.DataSource = txn;
            GridView1.DataBind();
        }
    }
}

网格显示数据。我应该如何编写查询?

将值从labelbox传递给linq查询

数据绑定不适用于原始LINQ查询。

你需要写GridView1.DataSource = txn.ToList();

在panno上添加断点以检查是否有从lblCardNo.Text传递的值。

string panno = lblCardNo.Text;         <-- here
var txn = (from p in query.TRANSACTIONs
          where Convert.ToString(p.PAN.ToString) == Convert.ToString(panno)    <-- here
          select p).ToList();
            GridView1.DataSource = txn;
            GridView1.DataBind();
then try to point to txn and check' results view' if it has a data.