优化这段代码: var surGivenName = RemoteHelper.GetNameInfo(userName); var listData = listAllUsers?.Select(c => new DtEmployee { OutEmployeeId = c.LoginId, SurName = surGivenName.SurName, GivenName = surGivenName.GivenName, Gender = c.Sex == "F" ? (int)Sex.女 : (int)Sex.男, ReservationType = (int)ReservationType.仅限本人, CredentialType=(int)CredentialType.身份证, Email = c.Email, Mobile = c.MobilePhone, IdCard=c.IDCard, WorkingState = string.Equals(c.Status, "enabled", StringComparison.OrdinalIgnoreCase) ? (int)WorkingState.在职 : (int)WorkingState.离职 }).ToList();
时间: 2024-02-10 17:25:42 浏览: 134
为了优化这段代码,可以考虑将获取 surGivenName 的操作放在循环外面,避免重复调用 RemoteHelper.GetNameInfo(userName) 方法。
可以将 surGivenName.SurName 和 surGivenName.GivenName 存储到一个临时变量中,然后在循环中使用该变量。
另外,可以使用三元运算符简化 c.Sex == "F" ? (int)Sex.女 : (int)Sex.男 的写法。
下面是优化后的代码:
var surGivenName = RemoteHelper.GetNameInfo(userName);
var surName = surGivenName.SurName;
var givenName = surGivenName.GivenName;
var listData = listAllUsers?.Select(c => new DtEmployee
{
OutEmployeeId = c.LoginId,
SurName = surName,
GivenName = givenName,
Gender = c.Sex == "F" ? (int)Sex.女 : (int)Sex.男,
ReservationType = (int)ReservationType.仅限本人,
CredentialType = (int)CredentialType.身份证,
Email = c.Email,
Mobile = c.MobilePhone,
IdCard = c.IDCard,
WorkingState = string.Equals(c.Status, "enabled", StringComparison.OrdinalIgnoreCase) ? (int)WorkingState.在职 : (int)WorkingState.离职
}).ToList();
这样,代码就避免了重复调用 RemoteHelper.GetNameInfo(userName) 方法,提高了效率。同时,使用临时变量存储 surGivenName.SurName 和 surGivenName.GivenName,使代码更简洁易读。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)