使用LINQ我怎么可以连接字符串属性从ITESM集合中 [英] using LINQ how can i concatenate string properties from itesm in a collection

查看:112
本文介绍了使用LINQ我怎么可以连接字符串属性从ITESM集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合中的对象的列表。每个对象有一个称为问题的字符串属性。我想从所有集合中的物品并置问题,并把它们放到一个字符串。什么是这样使用LINQ的最彻底的方法。



下面是手动方式:

 字符串issueList =; 
的foreach(在收集VAR项)
{
如果(!String.IsNullOrEmpty(item.Issue)
{
issueList = issueList + item.Issue +, ;
}
}
//删除最后一个逗号
issueList = issueList.Remove(issueList.Length - 2);
返回issueList;


解决方案

您可以写

 返回的string.join(,,collection.Select(O => o.Issue)); 

在NET 3.5的,你需要添加 .ToArray()


i have a list of objects in a collection. Each object has a string property called Issue. I want to concatenate the issue from all of the items in the collection and put them into a single string. what is the cleanest way of doing this using LINQ.

here is manual way:

 string issueList = "";
 foreach (var item in collection)
 {
       if (!String.IsNullOrEmpty(item.Issue)
       {
             issueList = issueList + item.Issue + ", ";
       }
 }
 //Remove the last comma
 issueList = issueList.Remove(issueList.Length - 2);
 return issueList;

解决方案

You can write

return String.Join(", ", collection.Select(o => o.Issue));

In .Net 3.5, you'll need to add .ToArray().

这篇关于使用LINQ我怎么可以连接字符串属性从ITESM集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆