【问题标题】:Return DataTable from Entity Query从实体查询返回 DataTable
【发布时间】:2012-06-18 09:08:47
【问题描述】:

我有这个代码:

public IList<T_LOGO> Get_All_Obj()
        {
            try
            {
                IList<T_LOGO> LesListe;
                using (Soft8Exp_ClientEntities oEntite_T = new Soft8Exp_ClientEntities())
                {
                    var query = from o in oEntite_T.T_LOGO select o;
                    LesListe = query.ToList();
                }
                return LesListe;
            }
            catch (Exception excThrown)
            {
                throw new Exception("Err_02", excThrown);
            }
        }

如何改为返回 DataTable?

public DataTable Get_All_Obj_DataTable()
        {
            try
            {
                DataTable TheTable = new DataTable("sd");
                using (Soft8Exp_ClientEntities oEntite_T = new Soft8Exp_ClientEntities())
                {
                    var query = from o in oEntite_T.T_LOGO select o;
                    IDbCommand cmd = Soft8Exp_ClientEntities.GetCommand(query as IQueryable); // ERROR GetCommand Not Found
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = (SqlCommand)cmd;

                }
                return TheTable;
            }
            catch (Exception excThrown)
            {
                throw new Exception("Err_02", excThrown);
            }
        }

【问题讨论】:

  • 为什么需要一个数据表?通常,强类型列表是首选。

标签: linq entity-framework datatable


【解决方案1】:

试试这个:

var query = (ObjectQuery<T_LOGO>)(from o in oEntite_T.T_LOGO select o);
Var command = new SqlCommand(query.ToTraceString(),
                             ((EntityConnection)oEntite_T.Connection).StoreConnection);

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2021-04-23
    • 2010-11-06
    • 2019-06-12
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多