如何使用linq to实体检查最后10个条目中的值
我有一个方法,需要使用EF检索数据库中的最后十个条目,并检查值和当前项之间是否匹配。以下是我到目前为止的情况如何使用linq to实体检查最后10个条目中的值,linq,entity-framework,Linq,Entity Framework,我有一个方法,需要使用EF检索数据库中的最后十个条目,并检查值和当前项之间是否匹配。以下是我到目前为止的情况 public static int ValidatePassword(string username, string password, int securityUserId) { int validResult = 0; /*Need to pass to client a value based upon
public static int ValidatePassword(string username, string password, int securityUserId)
{
int validResult = 0;
/*Need to pass to client a value based upon success or failure of validation
* 0 - success
* 1 - password has already been used in the last 10 entries
* 2 - password does not meet CJIS requirements
*/
IEnumerable<string> oldpassword = null;
// Create a Regular Expression to determine whether or not special characters are present.
Regex regularExpression = new Regex("[^a-z0-9]");
//if id exists pull last ten passwords
if (securityUserId > 0)
{
long id = Convert.ToInt64(securityUserId);
using (var context = new SecurityEntities(string.Empty))
{
try
{
oldpassword = (from p in context.SecurityAudits
where p.SecurityUserId == id &&
p.OldPassword == password
orderby p.ActionDate descending
select p.OldPassword.Take(10).ToString()).ToList();
}
catch (Exception ex)
{
string err = string.Format("ValidateCJISPassword() was unable to return an object msg:{0}", ex.Message);
throw new Exception(err, ex.InnerException);
}
finally
{
context.Dispose();
}
}
}
else if (oldpassword == null)
{
//no matching record found now check other requirements
if ((password.Length >= DEFAULT_CJIS_PASSWORD_MIN_LENGTH) && regularExpression.IsMatch(password) && (password != username))
{
//success
validResult = 0;
}
else
{
//password does not meet standard CJIS requirements
validResult = 2;
}
}
else
{
//matching record was found
validResult = 1;
}
return validResult;
}
}
public static int ValidatePassword(字符串用户名、字符串密码、int securityUserId)
{
int validResult=0;
/*需要根据验证的成功或失败向客户传递一个值
*0-成功
*1-密码已在最近10个条目中使用
*2-密码不符合CJIS要求
*/
IEnumerable oldpassword=null;
//创建正则表达式以确定是否存在特殊字符。
正则表达式regularExpression=新正则表达式(“[^a-z0-9]”);
//如果id存在,则提取最后十个密码
如果(securityUserId>0)
{
long id=Convert.ToInt64(securityUserId);
使用(var context=newsecurityentities(string.Empty))
{
尝试
{
oldpassword=(来自context.SecurityAudits中的p
其中p.SecurityUserId==id&&
p、 OldPassword==密码
orderby p.ActionDate降序
选择p.OldPassword.Take(10.ToString()).ToList();
}
捕获(例外情况除外)
{
string err=string.Format(“ValidateCJISPassword()无法返回对象msg:{0}”,例如Message);
抛出新异常(err,例如InnerException);
}
最后
{
context.Dispose();
}
}
}
else if(oldpassword==null)
{
//未找到匹配记录,请检查其他要求
if((password.Length>=DEFAULT\u CJIS\u password\u MIN\u Length)&®ularExpression.IsMatch(password)&&(password!=username))
{
//成功
validResult=0;
}
其他的
{
//密码不符合标准CJIS要求
有效结果=2;
}
}
其他的
{
//找到了匹配的记录
validResult=1;
}
返回有效结果;
}
}
当前挂起的地方是查询在ToString()方法上引发异常
LINQ to Entities无法识别方法“System.String ToString()”方法,并且无法将此方法转换为存储表达式。
我还在学习EF和linq是如何工作的,所以我不确定这里最好的方法是什么。我应该试着把结果设置成除了数组以外的数组还是列表,还是有其他的方法?
提前感谢,
干杯,改变这个
oldpassword = (from p in context.SecurityAudits
where p.SecurityUserId == id &&
p.OldPassword == password
orderby p.ActionDate descending
select p.OldPassword.Take(10).ToString()).ToList();
对此
oldpassword = (from p in context.SecurityAudits
where p.SecurityUserId == id &&
p.OldPassword == password
orderby p.ActionDate descending
select p.OldPassword).Take(10).ToList();
问题是您的Take(10)
子句不是整个结果的一部分,而是在实际的linq语句中。。它走在最外面,占据了整个结果集的前10名。。然后执行ToList()
,将整个内容转换为一个数组
下一个问题是您刚刚创建了一个数组并将其分配给oldpassword
我在这里没有看到任何与数组有关的东西
您需要执行以下操作:
oldpassword = (from p in context.SecurityAudits
where p.SecurityUserId == id &&
p.OldPassword == password
orderby p.ActionDate descending
select p.OldPassword.Take(10).ToString()).ToList();
对此
oldpassword = (from p in context.SecurityAudits
where p.SecurityUserId == id &&
p.OldPassword == password
orderby p.ActionDate descending
select p.OldPassword).Take(10).ToList();
问题是您的Take(10)
子句不是整个结果的一部分,而是在实际的linq语句中。。它走在最外面,占据了整个结果集的前10名。。然后执行ToList()
,将整个内容转换为一个数组
下一个问题是您刚刚创建了一个数组并将其分配给oldpassword
我在这里没有看到任何与数组有关的东西
您需要执行以下操作:
现在我已经了解了查询中需要的内容,我也可以按如下方式更新linq语句:
var lastTenPassword = (from p in context.SecurityAudits.Take(10)
orderby p.ActionDate descending
where p.SecurityUserId == id
select p.OldPassword).ToList();
string oldpassword = lastTenPassword.Where(a => a == password).FirstOrDefault();
测试更进一步,但现在通过在查询中移动.Take()方法,我明确地抓住了前十名,在这里,我第一次尝试检索所有记录,然后抓住了前十名
为了进行测试,您还可以看到我在何处使用了初始where(),首先按id获取所有记录,然后通过在该集合中查找匹配的密码对该集合执行筛选
再次感谢您的帮助现在我已经了解了查询中需要的内容,我也能够更新linq语句,如下所示:
var lastTenPassword = (from p in context.SecurityAudits.Take(10)
orderby p.ActionDate descending
where p.SecurityUserId == id
select p.OldPassword).ToList();
string oldpassword = lastTenPassword.Where(a => a == password).FirstOrDefault();
测试更进一步,但现在通过在查询中移动.Take()方法,我明确地抓住了前十名,在这里,我第一次尝试检索所有记录,然后抓住了前十名。