【问题标题】:working linq select method on a generic list of T?在 T 的通用列表上工作 linq select 方法?
【发布时间】:2015-04-17 12:41:03
【问题描述】:

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

这段代码无法编译,报错:

错误 1 ​​'TestInsertXMLvsTVP.TVP.TVPDataCollection' 不包含“选择”的定义,也没有扩展方法 'Select' 接受类型的第一个参数 'TestInsertXMLvsTVP.TVP.TVPDataCollection' 可以找到(您是否缺少 using 指令或程序集 参考?) 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();

    }

【问题讨论】:

  • 使用 System.Linq 添加
  • 这行不通,都需要使用

标签: c# .net linq


【解决方案1】:

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

using System.Linq;

【讨论】:

    【解决方案2】:

    我不知道为什么,但这工作正常:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多