将值从labelbox传递给linq查询

时间:2011-12-11 16:22:17

标签: c# asp.net linq

大家好,我是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();
        }

    }
}

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

在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.

此致