0

For Some debug i need to get the list of SqlDataRecord generated by the method IEnumerator IEnumerable.GetEnumerator() of the class TVPDataCollection

this code :

TVPDataCollection<AttributiDocumento> oAttributiDocumentoList = new  TVPDataCollection<AttributiDocumento>();

        AttributiDocumento doc1 = new AttributiDocumento();
        AttributiDocumento doc2 = new AttributiDocumento();

        oAttributiDocumentoList.Add(doc1);
        oAttributiDocumentoList.Add(doc2);

        var x = oAttributiDocumentoList.GetEnumerator();

x is a list of AttributiDocumento, but i need a list of SqlDataRecord because i want to inspect the values. what's the way to call the GetEnumerator() that returns SqlDataRecord?

public class TVPDataCollection<T> : List<T>, IEnumerable<SqlDataRecord> where T : class
{
    IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
    {
        List<SqlMetaData> records = new List<SqlMetaData>();
        var properties = typeof(T).GetProperties();
        foreach (var prop in properties)
        {
            SqlType oSqlType = GetSqlType(prop);
            if (oSqlType.UseSize)
                records.Add(new SqlMetaData(prop.Name, oSqlType.SqlDbType, oSqlType.Size));
            else
                records.Add(new SqlMetaData(prop.Name, oSqlType.SqlDbType));


        }

        SqlDataRecord oSqlDataRecord = new SqlDataRecord(records.ToArray());

        foreach (T data in this)
        {
            for (int i = 0; i < properties.Length; i++)
            {
                oSqlDataRecord.SetValue(i, properties[i].GetValue(data, null));

            }
            yield return oSqlDataRecord;
        }
    }
}
public class AttributiDocumento
{
    public int IdProvPart { get; set; }

    //uso stringa in quanto con data agigunge 2 ore del fuso orario

    public DateTime Data { get; set; }

    public int Articolo { get; set; }

    public int ExArticolo { get; set; }

    public int DocVer { get; set; }

    [LenAttribute(255)]
    public string Altro { get; set; }

    public AttributiDocumento()
    {
        this.Data = new DateTime(1900, 1, 1);
        this.Altro = string.Empty;
    }
}
FDB
  • 971
  • 16
  • 32

1 Answers1

2

To do exactly what you are asking for you need to cast oAttributiDocumentoList to the (IEnumerable<SqlDataRecord>) like this:

var x = ((IEnumerable<SqlDataRecord>)oAttributiDocumentoList).GetEnumerator();

But there are some disadvantages working with Enumerators.

Usually you need to access elements of your enumerable. To do so just use foreach loop.

foreach(var record in ((IEnumerable<SqlDataRecord>)oAttributiDocumentoList)) {
        //Do whatever you want with        record    
    }

If you still prefer to stick with .GetEnumerator(); do not forget to dispose it at the end or embrace in using.

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • 1
    You can cast and then directly use the result in a `for each`, rather then call `GetEnumerator`; this avoid explicitly having to work with enumerators and still getting the correct overload. – MicroVirus May 19 '15 at 09:25
  • Regarding your edit: `IEnumerator` is not disposable, so that last sentence makes no sense. – MicroVirus May 19 '15 at 09:32
  • @MicroVirus I thought it does: https://msdn.microsoft.com/en-us/library/78dfe2yb%28v=vs.110%29.aspx – George Mamaladze May 19 '15 at 09:36
  • Ahh, right, [`IEnumerator`](https://msdn.microsoft.com/en-us/library/system.collections.ienumerator%28v=vs.110%29.aspx) doesn't, but `IEnumerator` does. Interesting. – MicroVirus May 19 '15 at 09:37
  • 1
    @MicroVirus - could be interesting for you : http://stackoverflow.com/questions/232558/why-ienumerator-of-t-inherts-from-idisposable-but-non-generic-ienumerator-does – George Mamaladze May 19 '15 at 09:53