|
|
| Telerik OpenAccess ORM |
Send comments on this topic. |
| Querying the Metadata Container |
| See Also |
|
Programmer's Guide > Feature Reference > API > OpenAccess Metadata > Metadata Container > Querying the Metadata Container |
The purpose of this tutorial is to show you how to execute LINQ queries against the MetadataContainer, and to collect statistics about your domain model.
 |
All examples in this article will use the entities of the Northwind database. |
Example 1: Persistent Types
The following example shows a LINQ to Objects query which queries the metadata model and displays all the entity names.
| C# |
Copy Code |
|
var query = from meta in container.PersistentTypes select new { meta.Name, meta.Namespace, meta.IdentityType, meta.IsArtificial, meta.DataAccessKind }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.PersistentTypes Select New With {Key meta.Name, Key meta.Namespace, Key meta.IdentityType, Key meta.IsArtificial, Key meta.DataAccessKind} |

Example 2: Tables
Similarly to the previous example you could extract information about the tables in your database.
| C# |
Copy Code |
|
var query = from meta in container.Tables select new { meta.Name, meta.PKConstraintName }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.Tables Select New With {Key meta.Name, Key meta.PKConstraintName} |

Example 3: Inherited Persistent Types
You can also check inheritance and query the PersistentTypes which are derived from a base class.
| C# |
Copy Code |
|
var query = from meta in container.PersistentTypes select new { meta.Name, BaseTypeName = meta.BaseType != null ? meta.BaseType.Name : null }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.PersistentTypes Select New With {Key meta.Name, Key .BaseTypeName = If(meta.BaseType IsNot Nothing, meta.BaseType.Name, Nothing)} |
Example 4: Persistent Types and Properties
The following query will aggregate all properties and some of their characteristics like DefaultValue and Description.
| C# |
Copy Code |
|
var query = from meta in container.PersistentTypes let members = meta.Members select new { EntityName = meta.Name, MembersCount = members.Count, MemberNames = from p in members select new { p.Name, p.IsArtificial, p.Description, p.DataAccessKind } }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.PersistentTypes Let members = meta.Members Select New With {Key .EntityName = meta.Name, Key .MembersCount = members.Count, Key .MemberNames = From p In members Select New With {Key p.Name, Key p.IsArtificial, Key p.Description, Key p.DataAccessKind}} |
Example 5: Properties
The following example is absolutely the same as the previous example, however it flattens the result.
| C# |
Copy Code |
|
var query = from meta in container.PersistentTypes from p in meta.Members select new { EntityName = meta.Name, MembersCount = meta.Members.Count, FieldName = p.Name, IsArtificial = p.IsArtificial, Description = p.Description, DataAccessKind = p.DataAccessKind, MemberType = p.MemberType != null ? p.MemberType.Name : null }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.PersistentTypes , p In meta.Members Select New With {Key .EntityName = meta.Name, Key .MembersCount = meta.Members.Count, Key .FieldName = p.Name, Key .IsArtificial = p.IsArtificial, Key .Description = p.Description, Key .DataAccessKind = p.DataAccessKind, Key .MemberType = If(p.MemberType IsNot Nothing, p.MemberType.Name, Nothing)} |

Example 6: Properties
Now you have a simple collection with all the info about the properties. It is quite easy to query this collection again and to get all string properties that are not nullable.
| C# |
Copy Code |
|
var properties = from meta in container.PersistentTypes from p in meta.Members select new { EntityName = meta.Name, MembersCount = meta.Members.Count, FieldName = p.Name, IsArtificial = p.IsArtificial, Description = p.Description, DataAccessKind = p.DataAccessKind, MemberType = p.MemberType }; var query = from p in properties where p.MemberType != null && p.MemberType.ToString() == "String" select p; |
| VB.NET |
Copy Code |
|
Dim properties = From meta In container.PersistentTypes , p In meta.Members Select New With {Key .EntityName = meta.Name, Key .MembersCount = meta.Members.Count, Key .FieldName = p.Name, Key .IsArtificial = p.IsArtificial, Key .Description = p.Description, Key .DataAccessKind = p.DataAccessKind, Key .MemberType = p.MemberType} Dim query = From p In properties Where p.MemberType IsNot Nothing AndAlso p.MemberType.ToString() = "String" Select p |
Example 7: Properties
This data can also be used to create statistics. The following example will show how many properties of each (primitive) type are defined in your metadata model.
| C# |
Copy Code |
|
var properties = from meta in container.PersistentTypes from p in meta.Members select new { EntityName = meta.Name, MembersCount = meta.Members.Count, FieldName = p.Name, IsArtificial = p.IsArtificial, Description = p.Description, DataAccessKind = p.DataAccessKind, MemberType = p.MemberType != null ? p.MemberType.Name : null }; var query = (from p in properties group p by p.MemberType into gr select new { Type = gr.Key, Count = gr.Count() }) .OrderByDescending(t => t.Count); |
| VB.NET |
Copy Code |
|
Dim properties = From meta In container.PersistentTypes , p In meta.Members Select New With {Key .EntityName = meta.Name, Key .MembersCount = meta.Members.Count, Key .FieldName = p.Name, Key .IsArtificial = p.IsArtificial, Key .Description = p.Description, Key .DataAccessKind = p.DataAccessKind, Key .MemberType = If(p.MemberType IsNot Nothing, p.MemberType.Name, Nothing)} Dim query = ( From p In properties Group p By p.MemberType Into gr = Group Select New With {Key .Type = MemberType, Key .Count = gr.Count()}).OrderByDescending(Function(t) t.Count) |

Example 8: Relations
The metadata container also contains info about its relations.The following example shows you how to get all the metadata constraints.
| C# |
Copy Code |
|
var query = from meta in container.Constraints from columns in meta.SourceColumns select new { DestinationTable = meta.DestinationTable.Name, SourceTable = meta.SourceTable.Name, Name = meta.Name, SourceColumn = columns.Name }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.Constraints , columns In meta.SourceColumns Select New With {Key .DestinationTable = meta.DestinationTable.Name, Key .SourceTable = meta.SourceTable.Name, Key .Name = meta.Name, Key .SourceColumn = columns.Name} |
Example 9: Self-Referencing Relations
Maybe you are interested in all types which have self-referencing relations.
| C# |
Copy Code |
|
var query = from meta in container.Constraints where meta.SourceTable.Name == meta.DestinationTable.Name select new { meta.Name }; |
| VB.NET |
Copy Code |
|
Dim query = From meta In container.Constraints Where meta.SourceTable.Name = meta.DestinationTable.Name Select New With {Key meta.Name} |
See Also
|