在一个通用的T列表上工作linq select方法?

时间:2015-04-17 12:41:03

标签: c# .net linq

如何在通用的T列表中获得有效的linq select方法?

此代码无法编译,给出错误:

  

错误1' TestInsertXMLvsTVP.TVP.TVPDataCollection'   不包含'选择'的定义没有扩展方法   '选择'接受第一个类型的参数   ' TestInsertXMLvsTVP.TVP.TVPDataCollection'   可以找到(你错过了使用指令或程序集   参考?)D:\ Documenti \ Visual Studio   2012 \ Projects \ TestInsertXMLvsTVP \ TestInsertXMLvsTVP \ TestInsertXMLvsTVP \ frmTest.cs 134 26 TestInsertXMLvsTVP

public class TVPDataCollection<T> : List<T>, IEnumerable<SqlDataRecord>    where T : class,new()
{
    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;
        }
    }

    // change C# types to SqlDbType
    private SqlType GetSqlType(PropertyInfo Prop)
    {
        SqlType oReturn = new SqlType();
        Type type = Prop.PropertyType;

        if (type == typeof(Int32) || type == typeof(Nullable<Int32>))
        {
            oReturn.SqlDbType = SqlDbType.Int;
        }
        else if (type == typeof(Int64) || type == typeof(Nullable<Int64>))
        {
            oReturn.SqlDbType = SqlDbType.BigInt;
        }
        else if (type == typeof(Byte[]))
        {
            oReturn.SqlDbType = SqlDbType.Binary;
        }
        else if (type == typeof(Boolean) || type == typeof(Nullable<Boolean>))
        {
            oReturn.SqlDbType = SqlDbType.Bit;
        }
        else if (type == typeof(DateTime) || type == typeof(Nullable<DateTime>))
        {
            oReturn.SqlDbType = SqlDbType.DateTime;
        }
        else if (type == typeof(Decimal))
        {
            oReturn.SqlDbType = SqlDbType.Decimal;
        }
        else if (type == typeof(string))
        {
            oReturn.SqlDbType = SqlDbType.VarChar;
            LenAttribute[] lenAttributes = (LenAttribute[])Prop.GetCustomAttributes(typeof(LenAttribute), false);
            if (lenAttributes.Length > 0)
                oReturn.Size = lenAttributes[0].Len;
            else
                oReturn.Size = SqlMetaData.Max;
            oReturn.UseSize = true;

        }
        else
            throw new ApplicationException(string.Format("Tipo non trovato PropertyName:{0} Type:{1}", Prop.Name, type));
        // Please refer to the following document to add other types
        // http://msdn.microsoft.com/en-us/library/ms131092.aspx
        return oReturn;
    }
}

public class DocList
{
    public int IdDocumento { get;  set; }
}

void Test()
    {
        TVPDataCollection<DocList> list = new TVPDataCollection<DocList>();;

        string[] s = list.Select(x => x.IdDocumento.ToString()).ToArray();

    }

2 个答案:

答案 0 :(得分:3)

您需要在代码顶部添加以下代码:

using System.Linq;

答案 1 :(得分:0)

我不知道为什么但是这很好用:

string[] s = list.Select<DocList,string>(x => x.IdDocumento.ToString()).ToArray();