开发者

int array to IEnumerable<MyTestObj>?

开发者 https://www.devze.com 2022-12-19 14:40 出处:网络
I have a MVC that posts an array of integers, and I want to convert that array of int\'s to 开发者_如何学JAVAIEnumerable<MyTestObj>. How is that done? It seems I can\'t use myintArr.AsEnumerable

I have a MVC that posts an array of integers, and I want to convert that array of int's to 开发者_如何学JAVAIEnumerable<MyTestObj>. How is that done? It seems I can't use myintArr.AsEnumerable().


You need something along the lines of the following (depending how you create the object):

myIntArr.Select(i => new MyTestObj(i));
// or...
myIntArr.Select(i => (MyTestObj)i);
// or...
myIntArr.Select(i => new MyTestObj { SomeProperty = i });


I may be missing the point here, but doesn't an array support IEnumerable? In other words just use your array as it is (assuming you want an enumerable of int).

If you want an enumerable collection of some other object I think the other answers address that, but it's not clear (to me at least) what it is you are trying to acheive.


I'm a little confused. I am assuming that you have something like this for an action:

public ActionResult MyAction(int [] postedValues)

If that's the case then postedValues would already be IEnumerable. Not sure what problem you are having. It this isn't what you were asking then look at wither Mehrdad's or Andreas's answers.

0

精彩评论

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

关注公众号