首页 > 解决方案 > 需要 BLToolKIT 到实体框架的帮助

问题描述

我正在创建一种检查产品密钥的方法。在bltoolkit中一切正常,代码是

private void CheckKey()
        {
            try
            {
                using (DbManager db = new DbManager())
                {
                    DataTable dt = db
                    .SetCommand("SELECT TOP 1 * FROM TblReg WHERE ProductKey=@ProductKey",
                    db.Parameter("@ProductKey", CommanClass.strRegkey))
                    .ExecuteDataTable();
                    if (dt.Rows.Count == 0)
                    {
                        GetSoftKey = false;
                        strSoftKey = null;
                    }
                    else
                    {
                        strSoftKey = dt.Rows[0].Field<string>("ProductKey");
                        GetSoftKey = true;
                    }
                }

                if ((GetSoftKey == true) && (strSoftKey != null))
                {
                    if (strSoftKey == CommanClass.strRegkey)
                    {
                        SoftwareKey = true;
                    }
                    else
                    {
                        SoftwareKey = false;
                    }
                }
            }
            catch (Exception)
            {
                SoftwareKey = false;
            }
        }

现在,当我尝试使用实体框架编写方法来检查产品密钥时,我对如何将 DataTable 变量传递DataTable dt = login到实体上下文并设置实体查询参数感到困惑login.Parameter("@ProductKey", CommanClass.strRegkey),代码是

private void CheckKey()
        {
            try
            {
                using (loginEntities login = new loginEntities())
                {
                    var pKey= from pk in login.tblSoftRegs 
                              where pk.ProductKey == pk.ProductKey 
                              select pk.ProductKey.FirstOrDefault();
                    
                    if (pKey.Count() == 0)
                    {
                        GetSoftKey = false;
                        strSoftKey = null;
                    }
                    else
                    {
                        strSoftKey = ("ProductKey");
                        GetSoftKey = true;
                    }

                    
                }

                if ((GetSoftKey == true) && (strSoftKey != null))
                {
                    if (strSoftKey == CommanClass.busRegkey)
                    {
                        SoftwareKey = true;
                    }
                    else
                    {
                        SoftwareKey = false;
                    }
                }
            }
            catch (Exception)
            {
                SoftwareKey = false;
            }
        }

等待社区贡献...

标签: c#entity-frameworkmethodsdatatablebltoolkit

解决方案


你几乎明白了。如果没有找到,FirstOrDefault 将返回 null,并且您将局部变量直接绑定到您的 LINQ 查询中。像这样:

var pKey= from pk in login.tblSoftRegs 
          where pk.ProductKey == CommanClass.strRegkey
          select pk.ProductKey.FirstOrDefault();

if (pKey == null)
{
    GetSoftKey = false;
    strSoftKey = null;
}
else
{
    strSoftKey = ("ProductKey");
    GetSoftKey = true;
}

推荐阅读