-1

How can i make a datatype return IEnumerable<datatype> if it does not have AsEnumerable() method. For example:

FarPointSpread1.ActiveSheet.Rows[e.Row]

does not contain AsEnumerable() so i cannot do this

FarPointSpread1.ActiveSheet.Rows[e.Row].AsEnumerable()

e.Row is the current row in the spread's activesheet and i am expecting to be able to do something like this:

IEnumerable<FarPoint.Win.Spread.Row> = FarPointSpread1.ActiveSheet.Rows[e.Row].AsEnumerable();

I want to iterate over all fields in a row and set values on runtime - which will come from database. Is there a way i can achieve this? If there is some workaround please let me know with some examples. Even if the example cannot be shown using Farpoint - any other kind of example is welcome. Thanks in advance.

Rahul R.
  • 5,965
  • 3
  • 27
  • 38
Expert Novice
  • 1,943
  • 4
  • 22
  • 47
  • You don't really explain _what you are trying to do_, only that _what_ you are trying to do doesn't work. I would not expect to be able to iterate over a `Row` object. Doesn't it expose an enumerable `Cells` property? Or do you want to iterate over all rows? Then you shouldn't use the indexer `[e.Row]`, as that returns a single row. – CodeCaster Dec 02 '13 at 09:29
  • What do you expect to loop through a row? its cells? I don't really know through about the actual type of `Row` in your code, but if it has some cells collection, you can implement some extension method – King King Dec 02 '13 at 09:30
  • @CodeCaster - i want to be able to do this - `IEnumerable nthRow = fpSpread1.ActiveSheet.Cells[e.Row].AsEnumerable(); nthRow.ToList().ForEach(r => r.SetField( "A" , "Test" ); )` – Expert Novice Dec 02 '13 at 09:38
  • You're showing code again, not _explaining what you want it to do_. Do you want to iterate over all rows to set a field, or do you want to iterate over all fields in a row? – CodeCaster Dec 02 '13 at 09:39
  • @CodeCaster - i want to iterate over all fields in a row and set values on runtime - which will come from database – Expert Novice Dec 02 '13 at 09:39
  • Then see if the `Rows[e.Row]` object contains a `Cells` property. – CodeCaster Dec 02 '13 at 09:40
  • Even that does not have AsEnumerable... - i want to be able to use Linq - i do not want to iterate. – Expert Novice Dec 02 '13 at 09:41

1 Answers1

1

You can create an extension method. An example without FarPoint is:

public class TestClass
{
    public IReadOnlyCollection<int> Values { get; set; }
}

public static class TestClassExtensions
{
    public static IEnumerable<int> AsEnumerable(this TestClass cls)
    {
        return cls.Values.AsEnumerable();
    }
}

// Call
var cls = new TestClass();
var enum = cls.AsEnumerable();

This sample shows the general structure of an extension method - of course it is very simplified as TestClass contains the values already in a matching form. You'd implement the extension method so that it contains the logic to enumerable the rows for FarPoint.
As far as I understand your question, you want to extend a single row and enumerate the values of the fields. You'd use the data type of the row and enumerate the fields in the extension methods.

Markus
  • 20,838
  • 4
  • 31
  • 55