Created
September 30, 2011 21:54
-
-
Save jholt456/1255082 to your computer and use it in GitHub Desktop.
Telerik OpenAccess Context Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DataReaderExtensions | |
{ | |
public static List<dynamic> ToDynamicList(this IDataReader reader) | |
{ | |
var result = new List<dynamic>(); | |
while (reader.Read()) | |
{ | |
ExpandoObject item = new ExpandoObject(); | |
var itemDictionary = item as IDictionary<string, object>; | |
for (int i = 0; i < reader.FieldCount; i++) | |
{ | |
var key = reader.GetName(i); | |
if (!itemDictionary.ContainsKey(key)) | |
{ | |
itemDictionary.Add(key, reader[i]); | |
} | |
} | |
result.Add(item); | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%=Html.Telerik() | |
.Grid(Model) | |
.Name("dynamicGrid") | |
.Columns(factory => | |
{ | |
var firstItem = Model.FirstOrDefault(); | |
if(firstItem != null) | |
{ | |
var dict = firstItem as IDictionary<string, dynamic>; | |
if (dict != null) | |
{ | |
foreach (var key in dict.Keys) | |
{ | |
factory.Bound(key); | |
} | |
} | |
} | |
}) | |
.Sortable() %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Index() | |
{ | |
var context = new EntitiesModel(); | |
IEnumerable<dynamic> customersWithOrders= context.DynamicQuery("SELECT * FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID"); | |
return View(customersWithOrders); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class OpenAccessContextExtensions | |
{ | |
public static IEnumerable<dynamic> DynamicQuery(this OpenAccessContext context, string query) | |
{ | |
OAConnection oaConnection = context.Connection; | |
using (OACommand oaCommand = oaConnection.CreateCommand()) | |
{ | |
oaCommand.CommandType = CommandType.Text; | |
oaCommand.CommandText = query; | |
using (DbDataReader dataReader = oaCommand.ExecuteReader()) | |
{ | |
return dataReader.ToDynamicList(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment