如果list object为零,则设置另一个值

本文关键字:设置 另一个 list object 为零 如果 | 更新日期: 2023-09-27 18:17:31

我有一个列表,

List<Model.UserSetUp> objUserSetUp = objUserSetUp1.Select(m => new Model.UserSetUp()
{
    Id = m.UserId,
    FirstName = m.FirstName,
    SurName = m.SurName,
    Computer_Name = m.Computer_Name,
    IP_Address = m.IP_Address,
    LogInTime = m.LogInTime,
    UserName = Decrypt(m.UserName),
    Password = Decrypt(m.Password),
    login_Id = m.login_Id,
    UserType = "Documents Scanned",
    countID = m.docCount
 }).ToList();

从这个列表中,当Id为0时,则分配UserType="UnKnown"。

我该怎么做呢?

如果list object为零,则设置另一个值

Replace

UserType = "Documents Scanned" 

UserType = m.UserId != 0 ? "Documents Scanned" : "UnKnown"
List<Model.UserSetUp> objUserSetUp = objUserSetUp1.Select(m => new Model.UserSetUp()
        {
            Id = m.UserId,
            FirstName = m.FirstName,
            SurName = m.SurName,
            Computer_Name = m.Computer_Name,
            IP_Address = m.IP_Address,
            LogInTime = m.LogInTime,
            UserName = Decrypt(m.UserName),
            Password = Decrypt(m.Password),
            login_Id = m.login_Id,
            UserType = m.UserId == 0 ? "UnKnown" :"Documents Scanned",
            countID = m.docCount
        }).ToList();

设置列表时只需检查:

List<Model.UserSetUp> objUserSetUp = objUserSetUp1.Select(m => new Model.UserSetUp()
        {
            Id = m.UserId,
            FirstName = m.FirstName,
            SurName = m.SurName,
            Computer_Name = m.Computer_Name,
            IP_Address = m.IP_Address,
            LogInTime = m.LogInTime,
            UserName = Decrypt(m.UserName),
            Password = Decrypt(m.Password),
            login_Id = m.login_Id,
            UserType = m.UserId == 0 ? "Unknown" : "Documents Scanned",
            countID = m.docCount
        }).ToList();

您可以使用内联条件操作符:

UserType = (m.id != 0) ? "Documents Scanned" : "UnKnown",

可以使用三元操作符。

代替

UserType = "Documents Scanned",

可以输入

UserType = m.UserId == 0 ? UserType="UnKnown" :"Documents Scanned",