简体   繁体   中英

How to return the IEnumerable<T> Value using Datable?

I am trying to do return dataTable value from IEnumerable Type Anyone can help me For if i will pass any type for IEnumerable it will return that value

IEnumerable<T> enrichment = null;
var dataTable = new DataTable();

using (ReconContext context = new ReconContext())
{
    string sql = "SELECT * FROM " + DestinationTable + "  WHERE LoadId =" + ExternalLoadId;

    using (SqlDataAdapter adapater = new SqlDataAdapter(sql, context.Database.Connection.ConnectionString))
    {
        adapater.Fill(dataTable);
    }
}
return enrichment;

First of all you do not need to use generic definition unless you really need to. For general circumstances you can easily use DataRow like below;

IEnumerable<DataRow> enrichment = null;
var dataTable = new DataTable();
using (ReconContext context = new ReconContext())
{
    string sql = "SELECT * FROM " + DestinationTable + "  WHERE LoadId =" + ExternalLoadId;

    using (SqlDataAdapter adapater = new SqlDataAdapter(sql, context.Database.Connection.ConnectionString))
    {
        adapater.Fill(dataTable);
    }
}

enrichment = dataTable.AsEnumerable();
return enrichment;

Hope this helps.

Entity Framework already allows you to execute raw SQL queries and map their results to entities with SqlQuery . You don't need to use raw ADO.NET. You could just write:

public IEnumerable<T> LoadSomeLoads<T>(string table,int loadId)
{
    using (ReconContext context = new ReconContext())
    {
       var sq;="SELECT * FROM "+ table+ "  WHERE LoadId = @p0";
       var results= context.SqlQuery<T>(sql,loadId)
                           .ToArray();
    }
}

You should validate the table parameter to ensure it's an allowed table name, otherwise someone could query tables he has no authorization to, or simply browse for data by forcing the table name.

You avoid catastrophic injection attacks by using parameters but that doesn't prevent unauthorized disclosure if someone can force a query to an inappropriate table.

I suspect though that you should be looking to table-per-type inheritance. It sounds like you want to load different classes from different tables, where at least one property (LoadId) is common.

With Table-per-Type EF maps different types of an inheritance hierarchy to different tables and selects the correct table based on the mapping configuration.

Assuming, eg that you have the following classes:

public abstract class LoadTypeBase
{
    public int LoadId { get; set; }
    ...
}

[Table("LoadTableA")]
public class LoadTypeA : LoadTypeBase
{
    ...
}


[Table("LoadTableB")]
public class LoadTypeA : LoadTypeBase
{
    ...
}

and this context

public class ReconContext: DbContext
{
    ...
    public DbSet<LoadTypeBase> Loads { get; set; }
}

You could query for all load types with a specific LoadId with :

var query = from someLoad in context.Loads
            where someLoad.LoadId = loadId
            select someLoad;

You can restrict results to a specific type with OfType<T>()

var query = from someLoad in context.Loads.OfType<LoadTypeB>()
            where someLoad.LoadId = loadId
            select someLoad;

是的,这是正确的,但是如果我的功能喜欢这样,那么我将如何使用数据行,我想输出将通过的类型,将重新调整类型

 public IEnumerable<T> GeEnrichmendataByTableNameAndLoadId<T>(stringDestinationTable, int ExternalLoadId)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM