本文整理汇总了C#中ItemCollection.OfType方法的典型用法代码示例。如果您正苦于以下问题:C# ItemCollection.OfType方法的具体用法?C# ItemCollection.OfType怎么用?C# ItemCollection.OfType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ItemCollection
的用法示例。
在下文中一共展示了ItemCollection.OfType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
/// <summary>
/// Generates metadata for given item collection.
/// Fetches CLR models from object item collection.
/// </summary>
/// <param name="metadataWorkspace">The metadata workspace.</param>
/// <param name="itemCollection">The item collection.</param>
/// <param name="objectItemCollection">The object item collection.</param>
/// <param name="assembly">The assembly.</param>
/// <param name="connectionString">The connection string.</param>
/// <returns></returns>
/// <exception cref="BeetleException">Cannot load mapping information.</exception>
public static Metadata Generate(MetadataWorkspace metadataWorkspace, ItemCollection itemCollection,
ObjectItemCollection objectItemCollection, Assembly assembly, string connectionString) {
XDocument mappingXml = null;
XNamespace mappingNs = null;
try {
var csResourceMatch = Regex.Match(connectionString, @"res:*/(.*?\.msl)");
if (csResourceMatch.Success) {
var csResourceIndex = csResourceMatch.Value.LastIndexOf('/');
if (csResourceIndex >= 0) {
var csResource = csResourceMatch.Value.Substring(csResourceIndex + 1);
if (!string.IsNullOrEmpty(csResource)) {
using (var stream = assembly.GetManifestResourceStream(csResource)) {
if (stream != null) {
using (var reader = new StreamReader(stream)) {
mappingXml = XDocument.Load(reader);
// ReSharper disable once PossibleNullReferenceException
mappingNs = mappingXml.Root.GetDefaultNamespace();
}
}
}
}
}
}
}
catch {
mappingXml = null;
}
var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
// collect necessary entity information in one collection.
var entityResources = new List<EntityResource>();
foreach (var entityType in itemCollection.OfType<EntityType>().OrderBy(et => et.Name)) {
var et = entityType;
var entitySet = GetEntitySet(container, et);
IEnumerable<KeyValuePair<string, string>> propertyMappings = null;
XElement entityMap = null;
if (mappingXml != null) {
entityMap = mappingXml
.Descendants(mappingNs.GetName("EntitySetMapping"))
.First(esm => esm.Attribute("Name").Value == entitySet.Name)
.Descendants(mappingNs.GetName("EntityTypeMapping"))
.First(mf => {
var typeName = mf.Attribute("TypeName").Value;
return typeName == et.FullName || typeName == string.Format("IsTypeOf({0})", et.FullName);
})
.Descendants(mappingNs.GetName("MappingFragment"))
.Single();
propertyMappings = entityMap.Descendants(mappingNs.GetName("ScalarProperty"))
.Select(sp => new KeyValuePair<string, string>(sp.Attribute("Name").Value, sp.Attribute("ColumnName").Value));
}
var complexProperties = et.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == et);
Dictionary<EdmProperty, IEnumerable<ColumnMapping>> complexMappings = null;
if (entityMap != null) {
complexMappings = new Dictionary<EdmProperty, IEnumerable<ColumnMapping>>();
foreach (var complexProperty in complexProperties) {
var complexMapping = entityMap
.Descendants(mappingNs.GetName("ComplexProperty"))
.First(cp => cp.Attribute("Name").Value == complexProperty.Name)
.Descendants(mappingNs.GetName("ScalarProperty"))
.Select(p => new ColumnMapping(p.Attribute("Name").Value, p.Attribute("ColumnName").Value));
complexMappings.Add(complexProperty, complexMapping);
}
}
var entityResource = new EntityResource {
Entity = et,
Type = et,
Name = et.Name,
EntitySet = entitySet,
SimpleProperties = et.Properties
.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == et)
.ToDictionary(p => propertyMappings == null ? p.Name : propertyMappings.First(pm => pm.Key == p.Name).Value, p => p),
NavigationProperties = et.NavigationProperties.Where(np => np.DeclaringType == et),
ComplexProperties = complexMappings,
ClrType = objectItemCollection.GetClrType(objectItemCollection.OfType<EntityType>().First(x => x.Name == et.Name)),
TableName = entityMap == null ? et.Name : entityMap.Attribute("StoreEntitySet").Value
};
entityResources.Add(entityResource);
}
foreach (var complexType in itemCollection.OfType<ComplexType>().OrderBy(i => i.Name)) {
var ct = complexType;
var simpleProperties = ct.Properties
.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == ct)
.ToDictionary(p => p.Name, p => p);
var entityResource = new EntityResource {
//.........这里部分代码省略.........