-1

I have a enumerable like this (when I am debugging program says it is IEnumerable<String[]>. I do not know IEnumerable sufficiently):

var result = from p in displayedParticipants
             select new[] 
                    { 
                        p.Country, 
                        p.City,
                        p.SurveyStartDate.ToString("dd.MM.yyyy HH:mm"),
                        p.SurveyFinishDate.ToString("dd.MM.yyyy HH:mm"),
                        p.Device.ToString(),
                        p.IP.ToString(),
                        p.Longitude.ToString(),
                        p.Latitude.ToString(),                                                   
                    };

And I have an index array which indicates that which field is selected from this result, i.e the result object is the list of String array and each String array contains these fields.

string[] toShow = {0,1}. 

That means I want only to get 1st and 3rd fields (which are country and city) and remove all other fields. But IEnumerable<String[]> does not let me any remove operation.

How can I solve this problem? Thanks for any help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Harun Acar
  • 109
  • 1
  • 13
  • Possible duplicate of [How to access index in IEnumerator object in C#?](http://stackoverflow.com/questions/1654209/how-to-access-index-in-ienumerator-object-in-c) – Steffen Winkler Dec 18 '15 at 15:46
  • Problem is NOT accessing the element. I can access it via result.ElementAt(i) but I can not modify this element. – Harun Acar Dec 18 '15 at 15:48
  • IEnumerable interface does not offer any facility to modify it's elements, so you need to use a class which allows it as List – Gusman Dec 18 '15 at 15:49
  • 1
    Why is 1 the 3rd field? – Tim Schmelter Dec 18 '15 at 15:50
  • Please read the question and chosen answer. It'll solve your problem, since it is a duplicate. You should in most cases not work with an IEnumerable. – Steffen Winkler Dec 18 '15 at 15:50
  • @SteffenWinkler Please read OP's question: "But IEnumerable does not let me remove any operation"... this is not only about _accessing_, it's about _changing the content_ – René Vogt Dec 18 '15 at 16:01
  • @SteffenWinkler are sure you read my question ? Are you saying all questions related a tag is possible duplicate or you have some problem with me ? – Harun Acar Dec 21 '15 at 07:15
  • neither. I'm saying that, at least in C#, you should never ever ever work directly with an object of type IENumerable. First because you don't have to, second because you don't need to and third because it makes things overly complicated. Nearly every question that I've seen with both the C# and IENumerable tag could be solved the same way yours can: Use ToList() and be done with it. I've absolutely no idea why @RenéVogt would say that your question isn't a duplicate. Because it really is. – Steffen Winkler Dec 21 '15 at 19:27

2 Answers2

0

An IEnumerable is not something like a list or an array that contains something you can change. An IEnumerable is something that can enumerate objects.

But you can create a list (or an array) that matches your requirements out of this IEnumerable:

string[][] yourResults = 
    result.Select(inner => new[]{inner[0], inner[1]}).ToArray();
René Vogt
  • 43,056
  • 14
  • 77
  • 99
0

IEnumerable or IEnumerable<T> are interfaces that allow to enumerate a sequence, so they are not lists where you can add or remove items. You could use another query:

List<string[]> list = result
    .Select(arr => toShow.Select(i => arr[i]).ToArray())
    .ToList();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939