0

I am creating a method for checking the product key. everything working fine in bltoolkit the code is

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;
            }
        }

Now in When I try to write a method using entity framework for checking product key, it's confusing me at how to pass DataTable variable DataTable dt = login into entity context and set entity query parameter login.Parameter("@ProductKey", CommanClass.strRegkey), the code is

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;
            }
        }

Waiting for community contribution...

Ryk Tara
  • 5
  • 4
  • Look at `where pk.ProductKey == pk.ProductKey`. That's where to put it. But this looks like a covert request to fix all of your code because this can't possibly have run yet. – Gert Arnold Jan 17 '21 at 09:55
  • Can you figure out how to write 'DataTable dt = db .SetCommand("SELECT TOP 1 * FROM TblReg WHERE ProductKey=@ProductKey", db.Parameter("@ProductKey", CommanClass.strRegkey)) .ExecuteDataTable()' in entity framework – Ryk Tara Jan 17 '21 at 09:59

1 Answers1

0

You've almost got it. FirstOrDefault will return null if there are none found, and you bind local variables directly into your LINQ query. Like this:

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;
}
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67