简体   繁体   English

我如何将方法“utilities.DecryptStringFromBase64String”的调用限制为一次

[英]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.就我个人而言,我也会更改所有xysd以使其更有意义,但我现在让它们保持不变,因此您可以看到它是如何映射的。

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.

相关问题 如何限制DateTimePicker只更改日期,而不是时间? - How can I limit the DateTimePicker to only change the date, not the time? 如何避免文件创建 2 次,只有一个文件的内容应该编码 base64 字符串 - how I can avoid file creation 2 times, only one file which content should be encoded base64 string 如何在Update中仅调用一次方法? - How can I call a method inside Update only once? 如何将参数传递给LIMIT? 我可以将所有查询输出保存在一个字符串中吗? - How can i pass parameter to LIMIT ? Can i save all query output in one string? 如何限制每个请求只有一个调用的HttpModule? - How to limit HttpModule with ONLY ONE call per request? 如何仅调用方法中的return变量? - How can i call upon only the return variable in a method? 如何在ASMX Web服务中一次只允许一个调用者访问特定的类? - How can I limit access to a particular class to one caller at a time in an ASMX web service? 在Web应用程序中,什么时候我每24小时只能调用一次方法? - At which point in a web application can I call one method only once every 24 hours? 如何一次限制一个方法调用? - How to restrict to one method call at a time? 我如何只能运行浏览器一次 - How can I run my browser only one time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM