想传递字符串为true,列表有值

时间:2013-02-27 10:11:42

标签: c#

如果某个列表有值,我想将值传递为True,例如此处

如果item.Achievements.Count()有价值,那么我需要传递类似这样的内容

...
item.Location.City + "|" +True + "|" + item.Soid + "|" +
...

代码就像这样

foreach (var item in mgrProfile.GetMemberProfile(context.CurrentMember.MemberProfileSoid).Projects)
{
    bool isdataachivevement = 
    string list = item.Soid+ "|" +
                  item.Soid + "|" +
                  item.ProjectTitle + "|" +
                  item.Role + "|" +
                  item.StartDate.ToShortDateString() + "|" +
                  item.EndDate.value.ToShortDateString() + "|" +
                  item.Location.Country + "|" +
                  item.Location.State + "|" +
                  item.Location.City + "|" +
   // Here is I need to pass as True// if Achievements have data, otherwise false
                  item.Achievements.Count() + "|" +
                  item.Soid + "|" +
                  item.Soid + "|" +
                  item.Soid + "|" +
                  item.Soid + "|" +
                  projectlist.Add(list);
}

4 个答案:

答案 0 :(得分:2)

你可以做到

(.Count() > 0).toString();

答案 1 :(得分:1)

只需使用:

string list = item.Soid+ "|" +
              item.Soid + "|" +
              item.ProjectTitle + "|" +
              item.Role + "|" +
              item.StartDate.ToShortDateString() + "|" +
              item.EndDate.value.ToShortDateString() + "|" +
              item.Location.Country + "|" +
              item.Location.State + "|" +
              item.Location.City + "|" +
              item.Achievements.Any() + "|" + // <--- Here is a solution
              item.Achievements.Count() + "|" +
              item.Soid + "|" +
              item.Soid + "|" +
              item.Soid + "|" +
              item.Soid + "|" +
              projectlist.Add(list);

这将在ToString()bool)上致电System.Boolean,该bool.TrueString将返回bool.FalseString或{{1}}。

答案 2 :(得分:0)

替换为:

(item.Achievements.Count() > 0) ? "true" : "false"
如果您使用.NET列表

,请在评论中建议

或更好

item.Achievements.Any()

答案 3 :(得分:0)

Count是一个属性:

...
item.Location.City + "|" +
item.Achievements.Count > 0 ? "true" : "false" + "|" +
item.Soid + "|" +
...