/// <summary> /// 根据不同的实体和其ID来获取信息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="ID">因为表中都有[ID]列-所以这里不用传列名{如果你的主键是autoid-那就要另外处理了...}</param> /// <returns></returns> public IList<T> getInfoByID<T>(T entity, int ID) { //EF生成的--名字在生成EF模型的时候自己填写的↓ DBEntities db = new DBEntities(); //动态执行SQL语句 IList<T> retList = db.Database.SqlQuery<T>("SELECT * FROM " + typeof(T).ToString().Split('.')[1] + " WHERE ID=" + ID).ToList(); return retList; }
刚开始搞EF和Linq...请赐教...
下面是调用方法...↓
int ID = 1; IList<TableName> query = getInfoByID(new TableName(), ID);