如果列表对象为零,则设置另一个值

时间:2016-11-12 15:08:31

标签: c#

我有一个List,

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 =&#34; UnKnown&#34;。

我该怎么做?

5 个答案:

答案 0 :(得分:3)

替换

UserType = "Documents Scanned" 

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

答案 1 :(得分:2)

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();

答案 2 :(得分:1)

设置列表时只需检查一下:

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();

答案 3 :(得分:1)

您可以使用inline condition operator

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

答案 4 :(得分:1)

你可以使用三元运算符。

而不是

#+mygraph='svg', dev='svg',echo=FALSE,fig.height=4, fig.width=8,out.width = "400px",out.height="400px"

你可以输入

UserType = "Documents Scanned",