C# 希望将字符串传递为true,因为列表中有值

C# 希望将字符串传递为true,因为列表中有值,c#,C#,如果某个列表有值,我希望将值传递为True,例如这里 如果item.acquisitions.Count有值,那么我需要传递类似这样的内容 ... item.Location.City + "|" +True + "|" + item.Soid + "|" + ... 代码是这样的 foreach (var item in mgrProfile.GetMemberProfile(context.CurrentMember.MemberProfileSoid).Projects) { b

如果某个列表有值,我希望将值传递为True,例如这里

如果item.acquisitions.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);
}
将线路替换为:

(item.Achievements.Count() > 0) ? "true" : "false"
或者,如果您使用的是.NET列表,则最好按照评论中的建议

item.Achievements.Any()
计数是一个属性:

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

.计数>0.t字符串

只需使用:

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

这将调用bool System.Boolean上的ToString,它将返回bool.TrueString或bool.FalseString。

有什么问题或错误没有得到问题您不能直接输入:…+achievementsHaveData?true:false…Late Edit.Count>0.ToString@Sayse将其作为答案发布。更好的选择是使用item.aggregations.Any…尽管要了解它有多大的区别,您必须对其进行分析。如果aggregations是一个IEnumerable,那么它就不是一个属性。@OscarMederos它是一个列表。请仔细阅读该问题:如果某个列表具有值运算符“>”不能应用于“string”和“int”类型的操作数@SamElliott,我希望将值传递为True,这是因为他在长表达式中缺少带+的括号。这是正确的。其他问题如:a==b?true:false而不是a==b:i Get error at Nullable对象必须有一个值。在item.productions.Count>0.ToString时;不,对不起,我的错误应该是这样的。计数>0.toString;