开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜