2

I am reading in data from an Excel sheet so I have this code to get the region I want;

Excel.Range range = (Excel.Range)worksheet.get_Range("A9", "AO" + (worksheet.UsedRange.Rows.Count).ToString());
System.Array rangeCells = (System.Array)range.Cells.Value;

So my Data is x long and 41 cells wide so my array looks like;

rangeCells[1,1] through to rangeCells[1,41] is line 1
rangeCells[2,1] through to rangeCells[2,41] is line 2

and so forth until the end.

Does anyone know of a LINQ way to get each line into an object?

griegs
  • 22,624
  • 33
  • 128
  • 205
  • 2
    Why do you need LINQ? – I4V Jul 02 '13 at 21:49
  • 3
    What would the object look like? – p.s.w.g Jul 02 '13 at 21:51
  • 1
    @I4V Why do you need air to breathe? – Federico Berasategui Jul 02 '13 at 21:55
  • @I4V Why do I need linq? Because itterating through the whole collection building POCO's manually does not seem efficient. – griegs Jul 02 '13 at 21:58
  • p.s.w.g the object would look like a class that has 41 properties in it. The names of which, i don't think, are relevant to the question. – griegs Jul 02 '13 at 21:59
  • Efficient in _what way_? Efficient in how ugly/clean your code is and how easy it is to maintain? Or efficient in how fast it executes at runtime? – Chris Sinclair Jul 02 '13 at 21:59
  • @griegs It looks like you're using a multi-dimensional array, which it doesn't sound like linq can handle. http://stackoverflow.com/questions/2562817/selecting-a-multi-dimensional-array-in-linq – RandomWebGuy Jul 02 '13 at 21:59
  • @ChrisSinclair, I guess in terms of clean code. There are only going to be around 200 lines in this sheet so it's going to be fast no matter what. However I'd just like to have less code and less manual object creation. This isn't one of those questions that's going to make or break the application, this is a nice to have – griegs Jul 02 '13 at 22:01
  • @griegs So you'd have some kind of code like `new myObj() { Prop1 = rangeCells[1,0], Prop2 = rangeCells[1,1], SomeOtherProp = rangeCells[1,3], YetAnotherProp = rangeCells[1,4] .... };` for all 41 properties, but somehow condensed/cleaner/automated via LINQ (or other mechanism?) (EDIT: and of course incrementing the row) – Chris Sinclair Jul 02 '13 at 22:02
  • 4
    @HighCore Don't comment just to comment. Linq is good but not necessary. griegs, Linq wouldn't make your code more efficent. – I4V Jul 02 '13 at 22:02
  • @RandomWebGuy, it is a multidimension array yeah and by the looks of that link, and my own trials, i think you may be right. write this as an answer – griegs Jul 02 '13 at 22:02
  • @I4V, Oh I know it won't be more efficient, like I said, this is a nice to have to make the code look a little cleaner is all. – griegs Jul 02 '13 at 22:04
  • @griegs Would it really? Seems like two nested loops that `yield return` the object you want would be clean enough. Certainly cleaner than the mess of `Enumerable.Range()`s to do so with LINQ methods. – millimoose Jul 02 '13 at 22:20
  • @griegs Actually, scratch that, apparently you can `foreach` over a multidimensional array. – millimoose Jul 02 '13 at 22:27
  • @millimoose, really? Hadn't even considered that. Taking a look now. And Yield might not be a bad approach either – griegs Jul 02 '13 at 22:35
  • 1
    @griegs Going by RandomWebGuy's links, it seems that multidimensional arrays do, however, implement non-generic `IEnumerable`. So `range.Cells.Value.Cast().Select()` is also an option. – millimoose Jul 02 '13 at 23:00
  • @millimoose, Ah, right. Just finished the loop and manual poco creation but will take a look at this later today. thanks again – griegs Jul 02 '13 at 23:30

1 Answers1

1

I don't believe this is possible because multi-dimensional arrays do not impliment IEnumerable

Discussed more:

Community
  • 1
  • 1
RandomWebGuy
  • 1,439
  • 11
  • 23
  • 2
    @AbeMiessler Seeing as LINQ-to-Objects is a set of extension methods over `IEnumerable(T)`, the fact that only onedimensional arrays implement that interface would be a sticking point. – millimoose Jul 02 '13 at 22:18
  • 1
    @KevinBabcock If by "ways around that" you mean doing something to flatten the array, then you're not really working with a 2D array anymore. (I admit this is splitting hairs, but the ambiguity alone means this answer isn't wrong per se, if not particularly helpful.) – millimoose Jul 02 '13 at 22:22
  • @millimoose agreed (removed my comment) – Kevin Babcock Jul 02 '13 at 22:35