从实体查询返回DataTable

时间:2012-06-18 09:08:48

标签: linq entity-framework datatable

我有这段代码:

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);
            }
        }

1 个答案:

答案 0 :(得分:0)

试试这个:

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);