c# - 需要 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;
}
}
等待社区贡献...
解决方案
你几乎明白了。如果没有找到,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;
}
推荐阅读
- arrays - 如何打印二维数组?
- node.js - 使用 MongoDB 驱动程序 3.6 在 Node.js 中运行查询的最佳实践?
- c - GtkTextView 文本着色未按预期工作
- python - 如何根据二进制视频帧中的近似形状过滤轮廓
- reactjs - 使用 Multiple Switch 在 reactjs 中提供 Blanc 组件
- python-3.x - Pydbus 更多通知同时处理
- reactjs - 如何保护 ASP.NET Core Web API 端点只允许来自 React 应用程序的请求?
- python - Google drive API:获取上传视频的可共享公开链接
- apache-spark - 使用 Spark 计划 JSON 的 DynamoDB 数据格式
- python - 语音识别崩溃