i have the following code to return a list of generics that i want to iterate through and as a result generate a comma separated list of values as a string.
public static List<ReportCriteriaItem> GetCurrentSelection(ReportWizardCriteriaType criteriaType)
{
return Criteria.CriteriaList.FindAll(delegate(ReportCriteriaItem item)
{ return item.CriteriaType == criteriaType; }
);
}
here is the definition for ReportCriteriaItem - the object i make a generic list out of... i think this is the key here, to get its "id" field into a CSV:
public class ReportCriteriaItem
{
[System.Xml.Serialization.XmlAttribute("id")]
public string Id { get; set; }
[System.Xml.Serialization.XmlAttribute("name")]
public string Name { get; set; }
[System.Xml.Serialization.XmlAttribute("value")]
public string Value { get; set; }
[System.Xml.Serialization.XmlAttribute("type")]
public ReportWizardCriteriaType CriteriaType { get; set; }
public ReportCriteriaItem() { }
public ReportCriteriaItem(ReportWizardCriteriaType criteriaType, string id, string name, string value)
{
this.Id = id;
this.Name = name;
this.Value = val开发者_如何学JAVAue;
this.CriteriaType = criteriaType;
}
}
can i use a for each loop to do this?
The simplest approach is to use string.Join
:
string joined = string.Join(",", GetCurrentSelection(...).Select(x => x.Value));
The exact details of what you do will depend on which version of C# and .NET you're using, but the above is just an example. If you can give us more details, we can give you more specific code.
(In particular, in .NET 3.5 you'll need to provide string.Join
with an array; in .NET 4 you don't need to. The version of .NET you're using is much more important than the fact that you're using ASP.NET.)
If you already have the list, then you can just use the String.Join
method:
var myList = GetCurrentSelection(ReportWizardCriteriaType.SomeCriteria);
var csv = String.Join(",",myList.ToArray());
Voila!
If you have .net 4, you can use String.Join<T>(string, IEnumerable<T>):
string csv = string.Join(", ", GetCurrentSelection(...));
The string items are generated from ReportCriteriaItem
by executing ToString()
on them. If the string value must be generated by accessing some other method or property of your ReportCriteriaItem (or if you are using a .net version smaller than 4), Jon Skeet's answer is the way to go.
string list = "";
foreach (ReportCriteriaItem item in GetCurrentSelection(...)) {
if (!string.IsNullOrEmpty(list)) list += ",";
list += item.???;
}
// do something with the list.
i like the join one line solutions but due to the nature of the beast, and the beast being my object i have to loop and pull the value of each generic item...
string csv = "";
foreach (ReportCriteriaItem item in GetCurrentSelection(criteriaType))
{
csv += item.Id + ",";
}
return csv.TrimEnd(",".ToArray() ) ;
Enjoy!
public static class DataHelper
{
public static string ToCsv<T>(string separator, IEnumerable<T> objectlist)
{
Type t = typeof(T);
PropertyInfo[] fields = t.GetProperties();
string header = String.Join(separator, fields.Select(f => f.Name).ToArray());
StringBuilder csvdata = new StringBuilder();
csvdata.AppendLine(header);
foreach (var o in objectlist)
csvdata.AppendLine(ToCsvFields(separator, fields, o));
return csvdata.ToString();
}
public static string ToCsvFields(string separator, PropertyInfo[] fields, object o)
{
StringBuilder linie = new StringBuilder();
foreach (var f in fields)
{
if (linie.Length > 0)
linie.Append(separator);
var x = f.GetValue(o);
if (x != null)
linie.Append(x.ToString());
}
return linie.ToString();
}
}
Use it like this
var customers = new List<Customer>(); // Any simple class
string csv = DataHelper.ToCsv(";",customers );
精彩评论