[英]how I can limit the call to only one time for method "utilities.DecryptStringFromBase64String"
I am getting credential data from one of web service call and while decrypt it, the format is "username:::password".我从 web 服务调用之一获取凭证数据,解密时,格式为“用户名:::密码”。 I have other class as well and finally I am creating Data class like below,
我还有其他 class,最后我正在创建数据 class,如下所示,
var lstStudents = new List<Student>
{
new Student
{
Name = "studen1",
Credentials = new List<Credential> {new Credential {Key = "N1", Cred = "pNn/B3yUB+x2yiC310efoAjb8EkNhH1oD3NYF0v5SNxUKPtOtpxL21saVJGjmYPi" }, new Credential { Key = "N2", Cred = "" }}
},
new Student
{
Name = "studen2",
Credentials = new List<Credential> {new Credential {Key = "N1", Cred = "PT7CpnUcG7DIrJTxN8CcqoHAyTbNNST3DzGLpGQUHF6/jyooYKW1puXb/a+WX2M8" }, new Credential { Key = "N2", Cred = "" }}
},
};
var filterList = lstStudents.SelectMany(x => x.Credentials.Select(y =>
new Data
{
StudentName = x.Name,
CredentialKey = y.Key,
UserName = utilities.DecryptStringFromBase64String(y.Cred, key).Before(":::") ,
Password = utilities.DecryptStringFromBase64String(y.Cred, key).After(":::")
}))
.Where(d => d.CredentialKey == "N1")
.ToList();
Supporting classes,配套班级,
public class Data
{
public string StudentName { get; set; }
public string CredentialKey { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class Student
{
public string Name { get; set; }
public List<Credential> Credentials { get; set; }
}
public class Credential
{
public string Key { get; set; }
public string Cred { get; set; }
}
In above code I am calling method DecryptStringFromBase64String
2 times, I would like to call it only one time and prepare my data class, how to do this?在上面的代码中,我调用
DecryptStringFromBase64String
方法 2 次,我只想调用它一次并准备我的数据 class,该怎么做? Thanks!谢谢!
This will be easier if you tweak the query to use the actual LINQ language syntax (rather than the extension methods), as then you can use let
:如果您调整查询以使用实际的 LINQ 语言语法(而不是扩展方法),这将更容易,因为您可以使用
let
:
var filterList = (
from x in lstStudents
from y in x.Credentials
let s = utilities.DecryptStringFromBase64String(y.Cred, key)
let d = new Data
{
StudentName = x.Name,
CredentialKey = y.Key,
UserName = s.Before(":::"),
Password = s.After(":::")
}
where d.CredentialKey == "N1"
select d
).ToList();
or to do the where
sooner, to avoid some calculations / allocations:或者更快地做
where
,以避免一些计算/分配:
var filterList = (
from x in lstStudents
from y in x.Credentials
where y.Key == "N1"
let s = utilities.DecryptStringFromBase64String(y.Cred, key)
let d = new Data
{
StudentName = x.Name,
CredentialKey = y.Key,
UserName = s.Before(":::"),
Password = s.After(":::")
}
select d
).ToList();
Personally I'd also change all the x
, y
, s
, d
to be more meaningful, but I've left them the same for now, so you can see how it maps.就我个人而言,我也会更改所有
x
、 y
、 s
、 d
以使其更有意义,但我现在让它们保持不变,因此您可以看到它是如何映射的。
An alternative would be, as noted by @Sinatr in the comments, to use a lambda with body:正如@Sinatr 在评论中指出的那样,另一种方法是使用带有正文的 lambda:
var filterList = lstStudents.SelectMany(x => x.Credentials.Select(y => {
var s = utilities.DecryptStringFromBase64String(y.Cred, key);
return new Data {
StudentName = x.Name,
CredentialKey = y.Key,
UserName = s.Before(":::") ,
Password = s.After(":::")
};
}))
.Where(d => d.CredentialKey == "N1")
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.