C# MVC2 - Generic Collection to IEnumerable<string>
I can't seem to figure this out...a little help please, thank you vm!
I have a generic collection of Features. Each Feature has a FeatureId and FeatureName. I need to pass the featureids into an IEnumerable<string>
.
I thought I was close with this:
Listing.Features.ToArray().Cast<string>().AsEnumerable();
and even tried to 'MacGyver' it like
var sb = new System.Text.StringBuilder();
foreach (Feature f in Listing.Features)
{
sb.AppendFormat("{0}", f.FeatureId);
}
SelectedFeatures = sb.ToString().ToArray();
SelectedFeatures being the IEnumerable<string>
.
Am I getting close? I like the first attempt better as it's cleaner but am not picky anymore now that I'm stuck
If I understand what you need right, something like this:
var selectedFeatures = Listing.Features.Select(item=>item.FeatureId);
Or item.FeatureName
as appropriate. The example above gives you an IEnumerable<T>
where T==Item.FeatureId.GetType();
I found an older code sample in another project that helped me...this is how I got it working:
Model.SelectedFeatures.Select(c => c.FeatureId.ToString())
Thank you anyways, HTH some1
精彩评论