Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# T(); 对于(int i=0;istring.Equals(m.Name、fieldName、StringComparison.OrdinalIgnoreCase))) { 访问器[t,fieldName]=rd.GetValue(i); } } } 返回t; }_C#_List_Datareader - Fatal编程技术网

C# T(); 对于(int i=0;istring.Equals(m.Name、fieldName、StringComparison.OrdinalIgnoreCase))) { 访问器[t,fieldName]=rd.GetValue(i); } } } 返回t; }

C# T(); 对于(int i=0;istring.Equals(m.Name、fieldName、StringComparison.OrdinalIgnoreCase))) { 访问器[t,fieldName]=rd.GetValue(i); } } } 返回t; },c#,list,datareader,C#,List,Datareader,使用: public IEnumerable<T> GetResults<T>(SqlDataReader dr) where T : class, new() { while (dr.Read()) { yield return dr.ConvertToObject<T>()); } } 公共IEnumerable GetResults(SqlDataReader dr),其中T:class,new() { whil

使用:

public IEnumerable<T> GetResults<T>(SqlDataReader dr) where T : class, new()
{
    while (dr.Read())
    {
        yield return dr.ConvertToObject<T>());
    }
}
公共IEnumerable GetResults(SqlDataReader dr),其中T:class,new() { while(dr.Read()) { 返回dr.ConvertToObject()); } }
最简单的解决方案:

var dt=new DataTable();
dt.Load(myDataReader);
list<DataRow> dr=dt.AsEnumerable().ToList();
var dt=new DataTable();
dt.Load(myDataReader);
list dr=dt.AsEnumerable().ToList();
然后选择它们,以便将它们映射到任何类型。

对于.NET Core 2.0:

下面是一个扩展方法,它与.NET CORE 2.0配合使用,以执行原始SQL并将结果映射到任意类型的列表:

用法:

 var theViewModel = new List();
 string theQuery = @"SELECT * FROM dbo.Something";
 theViewModel = DataSQLHelper.ExecSQL(theQuery,_context);

 using Microsoft.EntityFrameworkCore;
 using System.Data;
 using System.Data.SqlClient;
 using System.Reflection;

public static List ExecSQL(string query, myDBcontext context)
 {
 using (context)
 {
 using (var command = context.Database.GetDbConnection().CreateCommand())
 {
 command.CommandText = query;
 command.CommandType = CommandType.Text;
 context.Database.OpenConnection();
                using (var result = command.ExecuteReader())
                {
                    List<T> list = new List<T>();
                    T obj = default(T);
                    while (result.Read())
                    {
                        obj = Activator.CreateInstance<T>();
                        foreach (PropertyInfo prop in obj.GetType().GetProperties())
                        {
                            if (!object.Equals(result[prop.Name], DBNull.Value))
                            {
                                prop.SetValue(obj, result[prop.Name], null);
                            }
                        }
                        list.Add(obj);
                    }
                    return list;

                }
            }
        }
    }
var theViewModel=newlist();
字符串theQuery=@“SELECT*FROM dbo.Something”;
viewmodel=DataSQLHelper.ExecSQL(查询,上下文);
使用Microsoft.EntityFrameworkCore;
使用系统数据;
使用System.Data.SqlClient;
运用系统反思;
公共静态列表ExecSQL(字符串查询,myDBcontext上下文)
{
使用(上下文)
{
使用(var命令=context.Database.GetDbConnection().CreateCommand())
{
command.CommandText=查询;
command.CommandType=CommandType.Text;
context.Database.OpenConnection();
使用(var result=command.ExecuteReader())
{
列表=新列表();
T obj=默认值(T);
while(result.Read())
{
obj=Activator.CreateInstance();
foreach(obj.GetType().GetProperties()中的PropertyInfo属性)
{
如果(!object.Equals(结果[prop.Name],DBNull.Value))
{
属性设置值(对象,结果[属性名称],null);
}
}
列表。添加(obj);
}
退货清单;
}
}
}
}

像魔术一样

我个人讨厌在构造函数中手动映射,我也不喜欢自己进行反射。这里有另一个解决方案,它是由奇妙的(并且相当普遍的)Newtonsoft JSON库提供的

只有当您的属性名与datareader列名完全匹配时,它才会起作用,但对我们来说效果很好

…假设您有一个datareader名称“yourDataReader”

var dt=new DataTable();
dt.Load(您的数据读取器);
//创建对象的json数组
字符串json=Newtonsoft.json.JsonConvert.SerializeObject(dt);
//这就是你要找的对吗??
列表=
Newtonsoft.Json.JsonConvert
.DeserializeObject(json);
我找到了这个解决方案

var cmd = ctx.Connection.CreateCommand();

T result = DbDataReaderdHelper.Fill<T>(cmd)

public static class DbDataReaderdHelper
{
    public static List<T> Fill<T>(DbCommand dbCommand) where T : new()
    {
        List<T> result = new List<T>();
        var reader = dbCommand.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Type type = typeof(T);
                T obj = (T)Activator.CreateInstance(type);
                PropertyInfo[] properties = type.GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    var value = reader[property.Name];

                    try
                    {
                        if (value != null)
                        {
                            var convertedValue = TypeDescriptor.GetConverter(property.PropertyType).ConvertFromInvariantString(value.ToString());

                            property.SetValue(obj, convertedValue);
                        }
                    }
                    catch {}
                }
                result.Add(obj);
            }
        }

        reader.Close();

        return result;
    }
}
var cmd=ctx.Connection.CreateCommand();
T result=dbdatareaderdheloper.Fill(cmd)
公共静态类DbDataReaderHelper
{
公共静态列表填充(DbCommand DbCommand),其中T:new()
{
列表结果=新列表();
var reader=dbCommand.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
{
类型=类型(T);
T obj=(T)Activator.CreateInstance(类型);
PropertyInfo[]properties=type.GetProperties();
foreach(属性中的PropertyInfo属性)
{
var value=reader[property.Name];
尝试
{
if(值!=null)
{
var convertedValue=TypeDescriptor.GetConverter(property.PropertyType).ConvertFromInvariantString(value.ToString());
设置值(obj,convertedValue);
}
}
捕获{}
}
结果:添加(obj);
}
}
reader.Close();
返回结果;
}
}
我的版本

用法:

var Q = await Reader.GetTable<DbRoom>("SELECT id, name FROM rooms");
Reader.GetTable包含:

            using (var R = await Q.ExecuteReaderAsync())
            {
                List<T> Result = new List<T>();

                Dictionary<int, PropertyInfo> Props = new Dictionary<int, PropertyInfo>();

                foreach (var p in typeof(T).GetProperties())
                {
                    for (int i = 0; i < R.FieldCount; i++)
                    {
                        if (p.GetCustomAttributes<ColumnAttribute>().FirstOrDefault(t => t.Name == R.GetName(i)) != null
                            && p.PropertyType == R.GetFieldType(i))
                        {
                            Props.Add(i, p);
                        }
                    }
                }

                while (await R.ReadAsync())
                {
                    T row = new T();

                    foreach (var kvp in Props)
                    {
                        kvp.Value.SetValue(row, R[kvp.Key]);
                    }

                    Result.Add(row);
                }

                return Result;
            }
使用(var R=wait Q.ExecuteReaderAsync())
{
列表结果=新列表();
字典道具=新字典();
foreach(typeof(T).GetProperties()中的var p)
{
对于(int i=0;it.Name==R.GetName(i))!=null
&&p.PropertyType==R.GetFieldType(i))
{
增加(i,p);
}
}
}
while(等待R.ReadAsync())
{
T行=新的T();
foreach(道具中的var kvp)
{
kvp.Value.SetValue(行,R[kvp.Key]);
}
结果。添加(行);
}
返回结果;
}

该死,要是我现在靠近编译器就好了,我很想写这段代码!如果没有人把我赶走的话,我明天就得拼命干了+1个问题。@MattHowells你仍然可以写它,我个人很想看看它是否有所不同。可能重复0投票否决非常好的答案。林克太酷了。因为你终于可以阅读这样的代码并欣赏它,所以对自己感觉良好是自大吗?不应该是“0否决票”!我投了赞成票。复制和粘贴魔鬼把我带到了这里。当我复制和粘贴它时,我得到了“找不到类型或命名空间名称“T”。我可能遗漏了一些明显的东西。想法?为什么是发电机?问题指定属性映射到列
public IEnumerable<T> GetData<T>(IDataReader reader, Func<IDataRecord, T> BuildObject)
{
    try
    {
        while (reader.Read())
        {
            yield return BuildObject(reader);
        }
    }
    finally
    {
         reader.Dispose();
    }
}

//call it like this:
var result = GetData(YourLibraryFunction(), Employee.Create);
public static class DataRecordHelper
{
    public static void CreateRecord<T>(IDataRecord record, T myClass)
    {
        PropertyInfo[] propertyInfos = typeof(T).GetProperties();

        for (int i = 0; i < record.FieldCount; i++)
        {
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.Name == record.GetName(i))
                {
                    propertyInfo.SetValue(myClass, Convert.ChangeType(record.GetValue(i), record.GetFieldType(i)), null);
                    break;
                }
            }
        }
    }
}

public class Employee
{
    public int Id { get; set; }
    public string LastName { get; set; }
    public DateTime? BirthDate { get; set; }

    public static IDataReader GetEmployeesReader()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

        conn.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID As Id, LastName, BirthDate FROM Employees"))
        {
            cmd.Connection = conn;
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
    }

    public static IEnumerable GetEmployees()
    {
        IDataReader rdr = GetEmployeesReader();
        while (rdr.Read())
        {
            Employee emp = new Employee();
            DataRecordHelper.CreateRecord<Employee>(rdr, emp);

            yield return emp;
        }
    }
}
<asp:GridView ID="GvEmps" runat="server" AutoGenerateColumns="true"></asp:GridView>

GvEmps.DataSource = Employee.GetEmployees();
GvEmps.DataBind();
public interface IDataReaderParser
{
    void Parse(IDataReader reader);
}

public class Foo : IDataReaderParser
{
    public string Name { get; set; }
    public int Age { get; private set; }

    public void Parse(IDataReader reader)
    {
        Name = reader["Name"] as string;
        Age = Convert.ToInt32(reader["Age"]);
    }
}

public class DataLoader
{
    public static IEnumerable<TEntity> GetRecords<TEntity>(string connectionStringName, string storedProcedureName, IEnumerable<SqlParameter> parameters = null)
                where TEntity : IDataReaderParser, new()
    {
        using (var sqlCommand = new SqlCommand(storedProcedureName, Connections.GetSqlConnection(connectionStringName)))
        {
            using (sqlCommand.Connection)
            {
                sqlCommand.CommandType = CommandType.StoredProcedure;
                AssignParameters(parameters, sqlCommand);
                sqlCommand.Connection.Open();

                using (var sqlDataReader = sqlCommand.ExecuteReader())
                {
                    while (sqlDataReader.Read())
                    {
                        //Create an instance and parse the reader to set the properties
                        var entity = new TEntity();
                        entity.Parse(sqlDataReader);
                        yield return entity;
                    }
                }
            }
        }
    }
}
IEnumerable<Foo> foos = DataLoader.GetRecords<Foo>(/* params */)
public static T ConvertToObject<T>(this SqlDataReader rd) where T : class, new()
{

    Type type = typeof(T);
    var accessor = TypeAccessor.Create(type);
    var members = accessor.GetMembers();
    var t = new T();

    for (int i = 0; i < rd.FieldCount; i++)
    {
        if (!rd.IsDBNull(i))
        {
            string fieldName = rd.GetName(i);

            if (members.Any(m => string.Equals(m.Name, fieldName, StringComparison.OrdinalIgnoreCase)))
            {
                accessor[t, fieldName] = rd.GetValue(i);
            }
        }
    }

    return t;
}
public IEnumerable<T> GetResults<T>(SqlDataReader dr) where T : class, new()
{
    while (dr.Read())
    {
        yield return dr.ConvertToObject<T>());
    }
}
var dt=new DataTable();
dt.Load(myDataReader);
list<DataRow> dr=dt.AsEnumerable().ToList();
 var theViewModel = new List();
 string theQuery = @"SELECT * FROM dbo.Something";
 theViewModel = DataSQLHelper.ExecSQL(theQuery,_context);

 using Microsoft.EntityFrameworkCore;
 using System.Data;
 using System.Data.SqlClient;
 using System.Reflection;

public static List ExecSQL(string query, myDBcontext context)
 {
 using (context)
 {
 using (var command = context.Database.GetDbConnection().CreateCommand())
 {
 command.CommandText = query;
 command.CommandType = CommandType.Text;
 context.Database.OpenConnection();
                using (var result = command.ExecuteReader())
                {
                    List<T> list = new List<T>();
                    T obj = default(T);
                    while (result.Read())
                    {
                        obj = Activator.CreateInstance<T>();
                        foreach (PropertyInfo prop in obj.GetType().GetProperties())
                        {
                            if (!object.Equals(result[prop.Name], DBNull.Value))
                            {
                                prop.SetValue(obj, result[prop.Name], null);
                            }
                        }
                        list.Add(obj);
                    }
                    return list;

                }
            }
        }
    }
        var dt = new DataTable();
        dt.Load(yourDataReader);
        // creates a json array of objects
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
        // this is what you're looking for right??
        List<YourEntityType> list = 
Newtonsoft.Json.JsonConvert
.DeserializeObject<List<YourEntityType>>(json);
var cmd = ctx.Connection.CreateCommand();

T result = DbDataReaderdHelper.Fill<T>(cmd)

public static class DbDataReaderdHelper
{
    public static List<T> Fill<T>(DbCommand dbCommand) where T : new()
    {
        List<T> result = new List<T>();
        var reader = dbCommand.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Type type = typeof(T);
                T obj = (T)Activator.CreateInstance(type);
                PropertyInfo[] properties = type.GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    var value = reader[property.Name];

                    try
                    {
                        if (value != null)
                        {
                            var convertedValue = TypeDescriptor.GetConverter(property.PropertyType).ConvertFromInvariantString(value.ToString());

                            property.SetValue(obj, convertedValue);
                        }
                    }
                    catch {}
                }
                result.Add(obj);
            }
        }

        reader.Close();

        return result;
    }
}
var Q = await Reader.GetTable<DbRoom>("SELECT id, name FROM rooms");
public class DbRoom
{
    [Column("id")]
    public int Id { get; set; }

    [Column("name")]
    public string Name { get; set; }
}
            using (var R = await Q.ExecuteReaderAsync())
            {
                List<T> Result = new List<T>();

                Dictionary<int, PropertyInfo> Props = new Dictionary<int, PropertyInfo>();

                foreach (var p in typeof(T).GetProperties())
                {
                    for (int i = 0; i < R.FieldCount; i++)
                    {
                        if (p.GetCustomAttributes<ColumnAttribute>().FirstOrDefault(t => t.Name == R.GetName(i)) != null
                            && p.PropertyType == R.GetFieldType(i))
                        {
                            Props.Add(i, p);
                        }
                    }
                }

                while (await R.ReadAsync())
                {
                    T row = new T();

                    foreach (var kvp in Props)
                    {
                        kvp.Value.SetValue(row, R[kvp.Key]);
                    }

                    Result.Add(row);
                }

                return Result;
            }